Xamarin form update listView itemSource

Ok I have a ListView object that has a List<Filiale> as an ItemSource , and I would like to update the ItemSource whenever the list of objects changes. ListView has a personalized ItemTemplate . At the moment, I have done this:

 public NearMe () { list=jM.ReadData (); listView.ItemsSource = list; listView.ItemTemplate = new DataTemplate(typeof(FilialeCell)); searchBar = new SearchBar { Placeholder="Search" }; searchBar.TextChanged += (sender, e) => { TextChanged(searchBar.Text); }; var stack = new StackLayout { Spacing = 0 }; stack.Children.Add (searchBar); stack.Children.Add (listView); Content = stack; } public void TextChanged(String text){ //DOSOMETHING list=newList; } 

As you can see in the TextChanged method, I assign a new list to the previous one, but there are no changes in the view. In the ViewCell that I created, I assign a shortcut text field using SetBinding

+8
c # xamarin xamarin.forms
source share
4 answers

Well, here's how I solved the problem, first of all I created a โ€œwrapperโ€ that implements INotifyPropertyChanged for the list that I took as an ItemSource, like this:

 public class Wrapper : INotifyPropertyChanged { List<Filiale> list; JsonManager jM = new JsonManager ();//retrieve the list public event PropertyChangedEventHandler PropertyChanged; public NearMeViewModel () { list = (jM.ReadData ()).OrderBy (x => x.distanza).ToList();//initialize the list } public List<Filiale> List{ //Property that will be used to get and set the item get{ return list; } set{ list = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("List"));// Throw!! } } } public void Reinitialize(){ // mymethod List = (jM.ReadData ()).OrderBy (x => x.distanza).ToList(); } 

Then in the NearMe class:

 Wrapper nearMeVM = new Wrapper(); public NearMe () { Binding myBinding = new Binding("List"); myBinding.Source = nearMeVM; myBinding.Path ="List"; myBinding.Mode = BindingMode.TwoWay; listView.SetBinding (ListView.ItemsSourceProperty, myBinding); listView.ItemTemplate = new DataTemplate(typeof(FilialeCell)); searchBar = new SearchBar { Placeholder="Search" }; searchBar.TextChanged += (sender, e) => { TextChanged(searchBar.Text); }; var stack = new StackLayout { Spacing = 0 }; stack.Children.Add (searchBar); stack.Children.Add (listView); Content = stack; } public void TextChanged(String text){ if (!String.IsNullOrEmpty (text)) { text = text [0].ToString ().ToUpper () + text.Substring (1); var filterSedi = nearMeVM.List.Where (filiale => filiale.nome.Contains (text)); var newList = filterSedi.ToList (); nearMeVM.List = newList.OrderBy (x => x.distanza).ToList (); } else { nearMeVM.Reinitialize (); } 
+3
source share

You can set the ItemsSource ListView to null and then set it again to reload the table. http://forums.xamarin.com/discussion/18868/tableview-reloaddata-equivalent-for-listview

+10
source share

You can define a base view model and inherit it from INotifyPropertyChanged

 public abstract class BaseViewModel : INotifyPropertyChanged { protected bool ChangeAndNotify<T>(ref T property, T value, [CallerMemberName] string propertyName = "") { if (!EqualityComparer<T>.Default.Equals(property, value)) { property = value; NotifyPropertyChanged(propertyName); return true; } return false; } protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; } 

then in your view model (JM example) will inherit from BaseViewModel and can create an ObservableCollection<YOURLISTCLASS> List

Also your fields in ViewModel (JM example) should be implemented as follows:

 public const string FirstNamePropertyName = "FirstName"; private string firstName = string.Empty; public string FirstName { get { return firstName; } set { this.ChangeAndNotify(ref this.firstName, value, FirstNamePropertyName); } } 

Hope this helps.

+2
source share

Modify the ObservableCollection list and implement INotifyPropertyChanged so that the changes are reflected in your ListView.

0
source share

All Articles