I have one ArrayList and it changes often.
The ListView displays the ArrayList's data so this ListView must be quickly changed when the ArrayList has changed.
So I wrote code like this:
ArrayList ar;
ListView lv;
paintmethod()
{
lv.items.clear();
lv.addlistview(ar);
lv.invalidate();
}
private void addlistview(ArrayList arr)
{
lv.Items.Add(arr.somedata.tostring(),arr.somedata.tostring());
}
This code works but when the Array开发者_如何学编程List has changed ListView is not refreshed immediately.
It's refreshed 20secs, 30secs or even 1 minute later.
How can I do more to solve this problem?
Try below and see any better. It's good practice to use BeginUpdate() and EndUpdate() when updating multiple listView items. Because BeginUpdate() prevents the control from drawing until the EndUpdate method is called.
paintmethod()
{
lv.BeginUpdate();
lv.items.clear();
lv.addlistview(ar);
lv.EndUpdate();
}
The preferred way to add multiple items to a ListView is to use the AddRange method of the ListView.ListViewItemCollection (accessed through the Items property of the ListView). This enables you to add an array of items to the list in a single operation.
MSDN
This should speeds up performance a lot.
Is there are reason you are not binding your listview.ItemsSource to an observablecollection? Then, you would only need to work against the observable collection, and that would notify the listview incremently.
this.SuspendLayout();
lv.items.clear();
lv.addlistview(ar);
lv.invalidate();
this.ResumeLayout(false);
精彩评论