Sam Mettenmeyer

Sam Mettenmeyer

  • NA
  • 108
  • 11.2k

Change value in a list during runtime

Feb 4 2019 4:39 AM
Hello everyone,
 
I have a button and a list displayed. When clicking on the button, I want to change the value of a specific item within that list. Please see following aimed output example:
 
[Program Start]
 
 Item Name Value
 Item Number 1
 Original Value
 Item Number 2
 Original Value
 Item Number 3
 Original Value
 
 [Button Click]
 
Item Name Value
Item Number 1
Original Value
Item Number 2
Changed Value
Item Number 3
Original Value
 
So how can I do this? What does my `private void OnButtonClick(object sender, EventArgs e)` have to look like? How can I access a specific entry in that list?
 
 To be better able to help me, I will share my code following:
  1. public class MyActivity : Activity  
  2. {  
  3.     List List = null;  
  4.   
  5.     protected override void OnCreate(Bundle bundle)  
  6.     {  
  7.         base.OnCreate(bundle);  
  8.   
  9.         SetContentView(Resource.Layout.MyActivity);  
  10.   
  11.         List = PopulateList();  
  12.         var lv = FindViewById(Resource.Id.list);  
  13.         var adapter = new ListAdapter(this, Resource.Layout.List, List);  
  14.         lv.Adapter = adapter;  
  15.   
  16.         FindViewById(Resource.Id.button).Click += OnButtonClick;  
  17.     }  

  1. class ListAdapter : ArrayAdapter  
  2. {  
  3.     List List;  
  4.     public ListAdapter(Context Context, int ListId, List List) : base(Context, ListId, List)  
  5.     {  
  6.         this.List = List;  
  7.     }  
  8.     public override int Count  
  9.     {  
  10.         get { return List.Count; }  
  11.     }  
  12.     public override View GetView(int position, View convertView, ViewGroup parent)  
  13.     {  
  14.         View v = convertView;  
  15.         if (v == null)  
  16.         {  
  17.             LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);  
  18.             v = inflater.Inflate(Resource.Layout.List, parent, false);  
  19.         }  
  20.         v.FindViewById(Resource.Id.name).Text = List[position].Name;  
  21.         v.FindViewById(Resource.Id.value).Text = "Original Value";  
  22.         return v;  
  23.     }  

 Using following *.axml-files:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout
  3. xmlns:android        ="http://schemas.android.com/apk/res/android"  
  4. android:orientation  ="vertical"  
  5. android:layout_width ="match_parent"  
  6. android:layout_height="match_parent">  
  7. <Button
  8.     android:layout_width ="match_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:text="Click Me"  
  11.     android:id="@+id/button" />  
  12. <ListView
  13.     android:layout_width="match_parent"  
  14.     android:layout_height="match_parent"  
  15.     android:id="@+id/list" />  
  16. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"       
  4. android:orientation="horizontal"       
  5. android:layout_width="fill_parent"       
  6. android:layout_height="fill_parent">          
  7. <TextView   
  8.         android:id="@+id/name"              
  9.         android:layout_width="wrap_content"            
  10.         android:layout_height="wrap_content" />  
  11. <TextView   
  12.         android:id="@+id/value"              
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content" />  
  15. </RelativeLayout>
 When simply doing
  1. private void OnButtonClick(object sender, EventArgs e)  
  2. {  
  3.    FindViewById(Resource.Id.value).Text = "Changed Value";  

 only the first item changes. So how to pass the position through the adapter? Can anyone maybe help?
 
Thanks in advance, 
 
Best regards
 
Edit: I found out that I have to do `adapter.NotifyDataSetChanged();` somewhere in the end. But when adding this, unfortunately nothing happens anymore when clicking the button.

Answers (1)