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

ObservableCollection in ListView not updating programatically

$
0
0

Hello everyone, I have a listview with an ObservableCollection of a class, I'm updating a property inside this class but the value isnt updating properly.

        private ObservableCollection<Place> _placesList;
        public ObservableCollection<Place> PlacesList { get { return _placesList; } set { _placesList = value; OnPropertyChanged("PlacesList"); ; } }

So, this class "Place" have a property

public string infoDisplay { get; set; }

That is used to display some info inside ListView, here is the View:

                <ListView
                    x:Name="MyListView"
                    Margin="5,0,0,0"
                    CachingStrategy="RecycleElement"
                    IsGroupingEnabled="false"
                    IsPullToRefreshEnabled="True"
                    IsRefreshing="{Binding IsRefreshing}"
                    IsVisible="{Binding IsListViewVisible}"
                    ItemsSource="{Binding PlacesList, Mode=TwoWay}"
                    RefreshCommand="{Binding RefreshListViewCommand}"
                    RowHeight="60">
                    <ListView.Behaviors>
                        <local1:ListViewTappedItemBehavior Command="{Binding PlaceTappedCommand}" Converter="{StaticResource SelectedItemConverter}" />
                    </ListView.Behaviors>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <ViewCell.ContextActions>
                                    <MenuItem
                                        Command="{Binding Path=BindingContext.SharePlaceCommand, Source={x:Reference Name=MyListView}}"
                                        CommandParameter="{Binding .}"
                                        Text="Compartilhar" />
                                    <MenuItem
                                        Command="{Binding Path=BindingContext.EditPlaceCommand, Source={x:Reference Name=MyListView}}"
                                        CommandParameter="{Binding .}"
                                        Text="Editar" />
                                </ViewCell.ContextActions>
                                <ViewCell.View>
                                    <Grid HorizontalOptions="StartAndExpand">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="0.2*" />
                                            <ColumnDefinition Width="1*" />
                                            <ColumnDefinition Width="1*" />
                                        </Grid.ColumnDefinitions>
                                        <Image
                                            Grid.Column="0"
                                            Aspect="AspectFill"
                                            HeightRequest="50"
                                            Source="{Binding ImageResourceId}"
                                            VerticalOptions="Center" />
                                        <Label
                                            Grid.Column="1"
                                            FontAttributes="Bold"
                                            FontSize="Large"
                                            Text="{Binding name}"
                                            TextColor="Black"
                                            VerticalTextAlignment="Center" />
                                        <Label
                                            Grid.Column="2"
                                            Margin="5,0,25,0"
                                            FontAttributes="Bold"
                                            FontSize="Large"
                                            HorizontalOptions="EndAndExpand"
                                            Text="{Binding infoDisplay}"
                                            TextColor="Black"
                                            VerticalTextAlignment="Center" />
                                    </Grid>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

This function "RefreshListViewAsync" is called when I pull to refresh the list, it get the List from SQlite.

        public async void RefresListViewAsync()
        {
            if (isCloud)
            {
                await CalculateDisplayInfo();
                try
                {
                    LocalSQLService BDx = new LocalSQLService(Settings.CurrentId, 0);
                    List<Place> resultado = BDx.ConsultarListaLugares();
                    if (resultado.Any() && resultado != null)
                    {
                        PlacesList = new ObservableCollection<Place>(resultado);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
            IsRefreshing = false;
        }

Here are 2 images to show before and after I pull to refresh:

The problem is:
If I pull to refresh the list view, it calls the function "RefreshListViewAsync" and update the data the way it should
If i dont pull to refresh and call the function "RefreshListViewAsync" programatically, it doesnt update the values. I checked to see if the values of "infoDisplay" property are updated and they have the values they should have, but dont display it correctly.

I have tried implementing INotifyCollectionChanged and didnt work also, any ideas?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



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