[英]mytimer in Listview items android xamarin
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).
我有一个包含多个项目的listview,项目是由用户添加的,并且在listview中有一个随机的位置(因为它们是由用户添加的)。 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;
};
how can update value of items?
如何更新物品的价值?
The recommended way is to create your own ArrayAdapter
and override the GetItem
like below: 推荐的方法是创建自己的
ArrayAdapter
并重写GetItem
如下所示:
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.