Error ListView UI thread, notifyDataSetChanged()

ElPiponElPipon COMember ✭✭

Hello everyone.

I have 2 buttons, search bar and a list. before go in to list, i filled the list. I use bindable to a Observable Collection in view model

when i press to any button of up, it should does a clear() in list and fill this again. And same inthe search bar. My problem is that after of i use 2 o 3 times the buttons or search bar this show the next error:

UNHANDLED EXCEPTION:
09-14 09:37:32.822 I/MonoDroid(23435): Java.Lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(-1, class android.widget.ListView) with Adapter(class android.widget.HeaderViewListAdapter)]

MY XAML is:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Prueba4La14.Pages.Prueba"
             Title="Lista Productos"
             xmlns:control="clr-namespace:Prueba4La14.Controls"
             BackgroundColor="White"
             BindingContext="{Binding Main, Source={StaticResource Locator}}">
    <StackLayout>
        <StackLayout Orientation="Horizontal" Padding="5,5,5,5">
            <Button
      Text="Chequeados"
      IsEnabled="{Binding IsEnabled2}"
      BackgroundColor="Red"
      TextColor="Black"
      BorderWidth="1"
      BorderColor="Black"
      Command="{Binding ChequeadoCommand}"
      HorizontalOptions="StartAndExpand"
      VerticalOptions="Start">

            </Button>
            <ActivityIndicator HorizontalOptions="Center" IsRunning="{Binding IsRunning2}" IsVisible="{Binding IsRunning2}" Color="Black"></ActivityIndicator>
            <Button
          Text="No Chequeados"
                IsEnabled="{Binding IsEnabled2}"
          BackgroundColor="Blue"
          TextColor="Black"
          BorderWidth="1"
          BorderColor="Black"
          Command="{Binding NoChequeadoCommand}"
          HorizontalOptions="Fill"
          VerticalOptions="End">

            </Button>
        </StackLayout>
        <SearchBar
            x:Name="searchbar"
            IsEnabled="{Binding IsEnabled2}"
            Placeholder="Buscar..."
            Text="{Binding Filter, Mode=TwoWay}"
            TextColor="Black"
            SearchCommand="{Binding SearchProductCommand}">
        </SearchBar>
        <ListView
            x:Name="listproducts"
            ItemsSource="{Binding Productos, Mode=TwoWay}"
            HasUnevenRows="True"
            ItemTapped="ListView_ItemTapped">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <control:SmallSimpleFastCell>

                        </control:SmallSimpleFastCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

I am using the library https://github.com/georgejecook/xamarinFastCell of @GeorgeCook

Can help me with the issue?

Posts

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    Can you show the View Model bound to the ListView?

  • ElPiponElPipon COMember ✭✭
    edited September 2017

    @ShantimohanElchuri, Thank you

    using System.Collections.ObjectModel;
    using GalaSoft.MvvmLight.Command;
    
    public class MainViewModel : INotifyPropertyChanged
        {
        public ObservableCollection<ProductItemViewModel> Productos { get; set; }
    
    
     public ICommand SearchProductCommand
            {
                get
                {
                    return new RelayCommand(SearchProduct);
                }
            }
    
     public ICommand ChequeadoCommand
            {
                get
                {
                    return new RelayCommand(SearchChequeado);
                }
            }
    
    

    Code Search Bar:

     public void SearchProduct()
            {
                IsRunning2 = true;
                Task.Run(() =>
                {
                    try
                    {
                        var da = new DataAccess();
                        responsee = GetProductsdetalle(Filter);
                        responsee2 = GetProducts(Filter);
    
                        Productos.Clear();
                        Log.State("Buscar producto (listado)", 1);
                        foreach (var product2 in responsee2)
                        {
                            if (!responsee.Exists(p => p.Codigo == product2.Codigo))
                            {
                                Productos.Add(new ProductItemViewModel
                                {
                                    IDListado = product2.IDListado,
                                    Codigo = product2.Codigo,
                                    EAN = product2.EAN,
                                    Descripcion = product2.Descripcion,
                                    Precio = "0"
                                });
                            }
                            else
                            {
                                Productos.Add(new ProductItemViewModel
                                {
                                    IDListado = product2.IDListado,
                                    Codigo = product2.Codigo,
                                    EAN = product2.EAN,
                                    Descripcion = product2.Descripcion,
                                    Precio = Convert.ToString(responsee.FirstOrDefault(p => p.Codigo == product2.Codigo).p_venta)
                                });
                            }
                        }
                        Log.State("Buscar producto (listado)", 2);
    
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(ex);
                    }
                });
                IsRunning2 = false;
            }
    

    Code button left:

     private async void SearchChequeado()
            {
                var da = new DataAccess();
                IsRunning2 = true;
                IsEnabled2 = false;
                await Task.Run(() =>
    
                {
    
                    try
                    {
    
                        responsee = dataservice.GetProductsCheck();
                        Log.State("Buscar chekeado (Listado)", 1);
                        Productos.Clear();
                        foreach (var product in responsee.Where(d => d.id_competidor == CompetidorCodigo && d.id_tipolistado == Id_Listado))
                        {
                            Productos.Add(new ProductItemViewModel
                            {
                                IDListado = product.id_tipolistado,
                                Codigo = product.Codigo,
                                EAN = product.EAN,
                                Descripcion = product.Descripcion,
                                Precio = Convert.ToString(product.p_venta),
                            });
    
                        }
                        Log.State("Buscar chekeado (Listado)", 2);
    
                    }
                    catch (Exception ex)
                    {
                        Log.Exception(ex);
                    }
                });
                IsEnabled2 = true;
                IsRunning2 = false;
            }
    }
    
Sign In or Register to comment.