ListView items removal doesn't work when IsPullToRefreshEnabled is set to true

LorenoArlaLorenoArla USMember ✭✭
edited August 2017 in Xamarin.Forms

Hello guys,

I have a really simple application with just a ListView and an ObserevableCollection (of 2 items of simple type Contact having some strings) bound to it.

I added some actions to this list, like removing selected item:

I added a handler to the Delete button:
void Delete_Clicked(object sender, System.EventArgs e)
{
var contact = (sender as MenuItem).CommandParameter as Contact; //here I get the contact to delete as Contact object
_contacts.Remove(contact); //this is my ObservableCollection holding objects of type Contact, which is ItemsSource of my ListView
}

It works as expected, items are removed when Delete is clicked.

The problem appeared when I added IsPullToRefreshEnabled="true" to ListView and handler for Refreshing event (it just refills my ObservableCollection with 2 items)
Deleting doesn't work anymore. Here's what I see in debugger after _contacts.Remove(contact) is executed:

My _contacts ObservableCollection still has 2 items, although 1 of them should be deleted - the one that should be gone displays the yellow information.

The important thing to note here, is that if I delete the item without IsPullToRefreshEnabled="true" deleting works as expected - debugger shows me just one item in my ObservableCollection after deleting another.

Later I observed another "interesting" thing - I launched the app again, this tie the problem was different. The items got deleted properly, but my refresh event did not work! My ObservableCollection is refilled as it should, but ListView still displays only one item (the other was deleted with Delete_Clicked handler). What's more, after refresh, I can't delete nothing anymore.

So I am really confused now. I have no idea what's going on. My application is really very simple. I am now thinking if investing time in Xamarin is a good idea, I don't think native iOS development would cause such problems.

Answers

  • ReemaGargReemaGarg INMember ✭✭
    edited August 2017

    Hi @LorenoArla ,
    Just Try this

    InteractiveListView.xaml:

    ListView ItemsSource="{x:Static local:InteractiveListView.items}" IsPullToRefreshEnabled="true" Refreshing="OnRefresh"

    InteractiveListView.cs :

    public static ObservableCollection items { get; set; }
    void OnRefresh (object sender, EventArgs e)
    {
    var list = (ListView)sender;
    //put your refreshing logic here
    var itemList = items.ToList();
    items.Clear ();
    foreach (var s in itemList) {
    items.Add (s);
    }
    //make sure to end the refresh state
    list.IsRefreshing = false;
    }

    void OnDelete (object sender, EventArgs e)
    {
    var item = (MenuItem)sender;
    items.Remove (item.CommandParameter.ToString());
    }

    Hope this will help you!

Sign In or Register to comment.