I have a listView where the cells have a field that is constantly updated on the backend. I have a timer set up to pull the new data at a set interval, but I don't see anywhere to have the listView refresh itself. For an iOS project I would simply call tableView.ReloadData(). I've tried reassigning the listView.ItemSource property on the main thread, but that didn't work.
I've also tried looking into bindings/observable collections, but those only seem to work for adding/removing items from the list. The number of items in my list aren't going to change, just a property on each item.
Here's a simplified version of the code I'm attempting:
public class Foo { List<MyItem> itemList; ListView listView; public Foo () { itemList = new List<MyItem>(); listView = new ListView(); listView.ItemTemplate = new DataTemplate (typeof(MyCell)); listView.ItemsSource = itemList; var timer = new Timer (2000); timer.Elapsed += OnTimerElapsed; timer.Start (); } public void OnTimerElapsed(object o, ElapsedEventArgs e) { Device.BeginInvokeOnMainThread (() => { itemList = beaconLocater.GetAvailableBeacons(); listView.ItemsSource = itemList; }); } }
public void OnTimerElapsed(object o, ElapsedEventArgs e) { Device.BeginInvokeOnMainThread (() => { itemList = beaconLocater.GetAvailableBeacons(); listView.ItemsSource = null; listView.ItemsSource = itemList; });
or turn ListView listView into ObservableCollection
Answers
or turn ListView listView into ObservableCollection
Setting the ItemsSource to null first worked, thanks!
Indeed, now, my ListView is in an Auto sized row of a grid.
how could I get the row resize properly to avoid clipping the newly taller ListView?
this is on ios in case the solution/workaround is platform specific
Thanks @Daniel Luberda. it worked for me
This SAVE me.....
It works!
the setting to null and resetting of the ItemsSource works but it's ugly (I have images in my ItemTemplate and they flicker when resetting) - surely there has to be a better way to update a single, targeted item? My situation is I have to update an image of a previously tapped item (not currently tapped) which I can identify and "update" but have found no good way to refresh. Thought I found the solution with a hack but alas it only works when the item that needs to be updated is in the current section of the scrollable view....sigh...
Resetting the itemSource worked for me thanks although I have a slightly different set up as I am using ReactiveList from ReactiveUI.