简体   繁体   中英

Why items on ListView in Xamarin do not disappear after object is removed from binded list, even if ItemSource property is reset?

I'm currently learning Xamarin and are at the stage of examining ListView. I've noticed that if I bind ListView to a List and remove an item from it, the listview will not display this change. I get it that I need to use ObservableCollection to have it work automatically (or have a collection implement proper interface), but I just would like to understand why doesn't it work even when i reset the ItemsSource property of ListView after the removal. Here's the code:

 namespace Lists
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ListsOne : ContentPage
    {
       List<Contact> _contacts = new List<Contact> {
                    new Contact{ Name = "Mosh", Number = "1234566", ImageUrl="http://lorempixel.com/100/100/people/1"},
                    new Contact { Name = "Josh", Number = "1236578" , ImageUrl = "http://lorempixel.com/100/100/people/2"}};

        IEnumerable<Contact> GetContacts(string searchText = null)
        {  

            if (String.IsNullOrWhiteSpace(searchText))
            {
                return _contacts;
            }

            return _contacts.Where(c => c.Name.StartsWith(searchText));
        }

        public ListsOne()
        {
            InitializeComponent();            
            listView.ItemsSource = GetContacts();
        }       

        private void Delete_Clicked(object sender, EventArgs e)
        {
            Contact contact = (sender as MenuItem).CommandParameter as Contact;
            _contacts.Remove(contact);
            listView.ItemsSource = GetContacts();

        }

        private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            listView.ItemsSource = GetContacts(e.NewTextValue);
        }
    }
}

As you can see in the Delete_Clicked method, after removing contact from _contacts, I reset the ItemsSource, but it will have no effect on the app, even though the pretty much same implementation for SearchBar_TextChanged works (if I recall correctly, am at work right now). Any insight on how it works? Sorry if it's dumb, but I am just learning.

Replace List with ObservableCollection. Latter one has event that notifies the UI about changes in it's array. (This is when you use MVVM, might not be applicable in in your example).

Also, as far as I know there is an issue with ListView.ItemSource when adding removing items to it. To make it work do this:

        private void Delete_Clicked(object sender, EventArgs e)
    {
        Contact contact = (sender as MenuItem).CommandParameter as Contact;
        _contacts.Remove(contact);
        listView.ItemsSource = null;
        listView.ItemsSource = GetContacts();

    }

In the document of listview-ItemsSource , it says:

If you want the ListView to automatically update as items are added, removed and changed in the underlying list, you'll need to use an ObservableCollection. ObservableCollection is defined in System.Collections.ObjectModel and is just like List, except that it can notify ListView of any changes:

ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
listView.ItemsSource = employees;

//Mr. Mono will be added to the ListView because it uses an ObservableCollection
employees.Add(new Employee(){ DisplayName="Mr. Mono"});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM