Change value of an item in a ListView during runtime

TBMSam

New member
Joined
Dec 12, 2018
Messages
3
Programming Experience
Beginner
Hello altogether,

In my program, I have a button and a list displayed. When clicking on the button, I want simply change the value of a specific item within that list. Please see following aimed output example:

[Program Start]

NameValue
Item Number 1Original Value
Item Number 2Original Value
Item Number 3Original Value

[Button Click]

NameValue
Item Number 1Original Value
Item Number 2Changed Value
Item Number 3Original Value

So how can I do this? What does my private void OnButtonClick(object sender, EventArgs e)-method have to look like? How can I access a specific entry in that list?

To be better able to help me, I please let me share my full code following:

Code:
public class MyActivity : Activity
{
   List List = null;
   protected override void OnCreate(Bundle bundle)
   {
       base.OnCreate(bundle);
       SetContentView(Resource.Layout.MyActivity);
       List = PopulateList();
       var lv = FindViewById(Resource.Id.list);
       var adapter = new ListAdapter(this, Resource.Layout.List, List);
       lv.Adapter = adapter;
       FindViewById(Resource.Id.button).Click += OnButtonClick;   
   }
}
Code:
class ListAdapter : ArrayAdapter
{
    List List;
    public ListAdapter(Context Context, int ListId, List List) : base(Context, ListId, List)
    {
        this.List = List;
    }
    public override int Count
    {
        get { return List.Count; }
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View v = convertView;
        if (v == null)
        {
            LayoutInflater inflater = (LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService);
            v = inflater.Inflate(Resource.Layout.List, parent, false);
        }
        v.FindViewById(Resource.Id.name).Text = List[position].Name;
        v.FindViewById(Resource.Id.value).Text = "Original Value";
        return v;
    }
}
Using following *.axml-files:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android        ="http://schemas.android.com/apk/res/android"
android:orientation  ="vertical"
android:layout_width ="match_parent"
android:layout_height="match_parent">
<Button
    android:layout_width ="match_parent"
    android:layout_height="wrap_content"
    android:text="Click Me"
    android:id="@+id/button" />
<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/list" />
</LinearLayout>
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<TextView
        android:id="@+id/value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
So how can I access the appropriate view/change it inside the private void OnButtonClick(object sender, EventArgs e)-method?
If I do
Code:
private void OnButtonClick(object sender, EventArgs e)
{
   FindViewById(Resource.Id.value).Text = "Changed Value";
}
only the first item changes. So how to pass the position through the adapter? How to change (in this example) only the second one (as shown in the beginning)? Can anyone maybe help?
Thanks in advance,

Best regards
 
Top Bottom