Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

ListView Item data binding on Android.

$
0
0

First things first, this isn't a problem on iOS, but is a problem on Android.

I have an object with several properties on it. The object and properties support INotifyPropertyChanged.

I have a collection of said objects in an ObservableCollection.

I have a ListView. The ListView's ItemsSource is set to the ObservableCollection. The ListView's ItemTemplate contains some labels that data bind to the fields in the above object.

So, roughly:

public class Item : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged( [CallerMemberName] String propertyName = "" )
        {
            var handler = PropertyChanged;
            if( handler != null )
            {
                handler( this, new PropertyChangedEventArgs( propertyName ) );
            }
        }

        private int _number;
        public int Number
        {
                get { return _number; }
                set 
                {
                        if( _number != value )
                        {
                                _number = value;
                                OnPropertyChanged();
                        }
                }
        }
}

public class ItemCollection : ObservableCollection<Item>
{
}

public class MyPage : ContentPage
{
        private ListView _lst;
        private ItemCollection _items;
        public MyPage()
        {
                ...
                _lst = new ListView();
                _lst.ItemsSource = _items;

                var cell = new DataTemplate( ()=>
                {
                        Label lMain = new Label();
                        lMain.SetBinding( Label.TextProperty, "Number" );

                        StackLayout text = new StackLayout();
                        text.Orientation = StackOrientation.Vertical;
                        text.HorizontalOptions = LayoutOptions.StartAndExpand;
                        text.Children.Add( lMain );

                        ...

                        ViewCell vc = new ViewCell();
                        vc.View = text;

                        return vc;
                }
                _lst.ItemTemplate = cell;
                Content = _lst;
        }
}

Now then, elsewhere in the code items get added to the list. This causes the list to update appropriately, and show the new item(s). The problem I'm having is that if a given item's data changes, the ListView never updates to reflect that change. So in the crude example above, if the value of Number changes, the list entry never changes the display. This ObservableCollection is saved to storage on the device, and if I exit the app and reload it, the list will reflect the new values -- just not while the app is running -- so I know they were set properly.

Is there something I can do here to force the update? Whether on the ObservableCollection or the ListView? I can't seem to find anything that says I can force an update on either of those, but I could be missing something...

Thanks.

(again, iOS seems to work as it should, and new values get updated on their respective list items...)


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>