Reputation: 1
I have no idea how to change the value of items in listview every second. I have a listview with several items, items are added by the user and have a random place in listview (because they are added by the user). For example, ram size, storage, etc. items every time have a new value. How can I update the value of the items?
My listview code:
items = new List<string>();
btn.Click += delegate
{
items.Add(myip.Text);
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleExpandableListItem1, items);
listnames.Adapter = adapter;
};
Upvotes: 0
Views: 102
Reputation: 10841
how can update value of items?
The recommended way is to create your own ArrayAdapter
and override the GetItem
like below:
public class MyArrayAdapter:ArrayAdapter
{
private Context context;
private int layoutId, textViewId;
List<string> mList;
public MyArrayAdapter(Context c,int layoutId,int textViewId, List<string> list) : base(c, layoutId,textViewId,list)
{
mList = list;
context = c;
this.layoutId = layoutId;
this.textViewId = textViewId;
}
public override Java.Lang.Object GetItem(int position)
{
return mList[position];
}
}
Then you can update the value at certain position by update the source List:
list[3] = "Edit Row";
adapter.NotifyDataSetChanged();
The other workaround is to remove the certain row and then insert a new row:
adapter.Remove(list[3]);
adapter.Insert("New Row", 3);
//the source list should also be updated.
list[3] = "New Row";
adapter.NotifyDataSetChanged();
Update:
To change the data every second you can use a recursive function to accomplish that:
int index=1;
...
void ChangeData()
{
Task.Delay(1000).ContinueWith(t => {
list[3] = "Edit Row" + index;
adapter.NotifyDataSetChanged();
index++;
ChangeData();//recursive happens here
},TaskScheduler.FromCurrentSynchronizationContext());
}
Upvotes: 1