ListView with ObservableCollection and ViewCell - not displaying after edit

JamesLaveryJamesLavery ✭✭✭✭✭GBBeta, University ✭✭✭✭✭
edited February 2015 in Xamarin.Forms

I've got a ListView bound to an ObservableCollection.

I'm using the ItemTapped event to navigate to a details page where the details of an item can be edited, and also using an 'add' button on the navigation bar to add a new item to the underlying list.

My problem is that after adding a new item, or editing an existing item (and navigating back to this page from the details page), the relevant item in the list is showing as blank - the ViewCell doesn't seem to be rendering properly.

If I navigate back off this page and then onto it again, the new and edited items show fine.

I have added a CollectionChanged handler for the ObservableCollection, and can see the event being fired.

Is this a known problem with ListViews? I'm using Xamarin.Forms V1.3.4, and the above is occurring on iOS - I haven't checked it on Android yet.

Answers

  • TektonTekton ✭✭✭ USMember ✭✭✭

    I have similar code, and I think I've had this problem too. Off the top of my head, I think I work around this by re-setting the ListView's ItemsSource/DataTemplate stuff. I can look a bit later today, into that code, to clarify this.

  • Christopher_MillerChristopher_Miller ✭✭ USUniversity ✭✭

    @JamesLavery, I'm seeing the same thing right now in 1.3.4. I'm testing with Windows Phone at the moment but the same problem. As a quick hack, I'm setting the ItemSource to null and back to the collection. It works, but the page flashes and the position is reset.

    I don't know if it's Windows Phone related, but the ListView.ScrollTo() method doesn't work either.

  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭

    @ChrisMiller I tried your suggestion and it doesn't fix it on iOS. In fact it makes it worse! I'm seeing the contents appear and then immediately refresh with blank cells even on first display of the page. The correct data is attached - if I tap on a cell, the details page contains the correct data.

    Where are you setting the ItemsSource to null and back to the collection? I assume in the Appearing event for the page. This is where I'm doing it - I can't see any other place to hook it in.

  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭
    edited February 2015

    I've just tested this on Android (in my Android project for the same app) and it works fine with no need to remove/add ItemsSource or anything.

    So it looks like a bug in the underlying iOS (and Windows Phone according to @ChrisMiller) renderer a custom ViewCell. Is this a known bug? If not I'll raise one.

  • Christopher_MillerChristopher_Miller ✭✭ USUniversity ✭✭

    It's not working correctly for me with Android, I get an exception that I have not had enough time to track down. The exception does not occur when I null out and restore the ItemSource. The ScrollTo method does work under Android, but not with Windows Phone.

  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭

    @ChrisMiller - Curiouser and curiouser. It's working for me on Android.

    Ah - I'm actually running Xamarin.Forms 1.3.2.0 - not 1.3.4.0 as I previousy said.

    I'll upgrade to 1.3.4.0 and see what happens.

  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭
    edited February 2015

    Right. Upgrading to 1.3.4.0 seems to have fixed this particular problem. iOS behaves correctly, as does Android.

    However, I am now seeing the problem detailed in forums.xamarin.com/discussion/33441/ios-listview-cell-remove-menuitem-stays-open-on-delete on iOS. (I tried linking to that thread, but the link kept resolving to this one!)

  • JamesLaveryJamesLavery ✭✭✭✭✭ GBBeta, University ✭✭✭✭✭
    edited February 2015

    Note that this is working without having to do anything clever with the ItemsSource for my ListView. However, I am supplying an ObservableCollection from my DL, and binding to it as follows in my ViewModel:

    ObservableCollection<BL.Venue> _Venues;
            public ObservableCollection<BL.Venue> Venues
            {
                get
                {
                    return _Venues ?? (_Venues = DL.VenueManager.List());
                }
            }
    

    Therefore the changes in my DL collection is automatically triggering the CollectionChanged event in my ViewModel and making my Page update correctly.

Sign In or Register to comment.