1

My goal is to make the update of a listview of records in real time.

in the load page i set:

List<Myobjetcs> myitems = new List<Myobjects>();

...
...
        listView.IsVisible = false;
        listView.RowHeight = 55;
        listView.IsPullToRefreshEnabled = true;
        listView.Refreshing += ListView_Refreshing;
        listView.ItemTapped += ListView_ItemTapped;
        listView.SeparatorVisibility = SeparatorVisibility.None;
        listView.SeparatorColor = Color.Black;
        listView.ItemsSource = myitems;

every 10 seconds (with API) they will update the data, only a few records, randomly. My goal is to update the item without doing a refresh of the listview ... and without the ItemSource set to null and then reassign data.

 public async void setSuperficie()
 {
      //here i receive the result of API (every 10 seconds) and update record listview. 
 }

I tried to slide the ItemSource with a for loop and update the data, but don't work.

it's possibile do ?

listView.ItemsSource[index].Property = Value;
Mr. Developer
  • 3,295
  • 7
  • 43
  • 110

1 Answers1

1

What is "myitems"?

You have to use a ObservableCollection

ObservableCollection<MyModel> myitems = new ObservableCollection<MyModel>();

then

 public async void setSuperficie()
 {
      //here i receive the result of API (every 10 seconds) and update record listview. 
      // here you have to modify the myitems collection, adding or removing items
      myitems.add(mynewitem);
 }

you should also implemente INotifyPropertyChanged. For this I suggest to use Fody

Alessandro Caliaro
  • 5,623
  • 7
  • 27
  • 52
  • 1
    myitems is a List myitems, maybe is better use observablecollection ? but how I update the record of list ? With your method i add the record on the list, but not update. ps.: sei Italiano ? – Mr. Developer Mar 17 '17 at 11:01
  • 1
    Brianzolo. Yes if you have to automatically update an UI with a ListView you don't have to use List but ObservableCollection. It solves your UI's update problems for Add and Delete. To Update an item (modify a property in your model) you have to use INotifyPropertyChanged: changing the property's value, it is automatically updated to UI https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/ – Alessandro Caliaro Mar 17 '17 at 11:22