Proper way to update the tableview's section's content?

laygrlaygr MXMember, University

I'm doing this:

Xamarin.Forms.Device.BeginInvokeOnMainThread (() =>
        {
            CitiesSection.Clear ();
            TerminalsSection.Clear ();
            CitiesSection.Add (PlacesVM.Cities.ConvertAll<TextCell> ((c) => new TextCell{ Text = c.Name }));
            TerminalsSection.Add (PlacesVM.Terminals.ConvertAll<TextCell> ((t) => new TextCell {
                Text = t.Name,
                Detail = t.City
            }));
        });

and getting this error:

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 xamarin.forms.platform.android.TableViewModelRenderer)]
[MonoDroid] at android.widget.ListView.layoutChildren(ListView.java:1555)
[MonoDroid] at android.widget.AbsListView.onTouchUp(AbsListView.java:3617)
[MonoDroid] at android.widget.AbsListView.onTouchEvent(AbsListView.java:3429)
[MonoDroid] at android.view.View.dispatchTouchEvent(View.java:7706)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at xamarin.forms.platform.android.PlatformRenderer.n_dispatchTouchEvent(Native Method)
[MonoDroid] at xamarin.forms.platform.android.PlatformRenderer.dispatchTouchEvent(PlatformRenderer.java:62)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
[MonoDroid] at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
[MonoDroid] at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
[MonoDroid] at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
[MonoDroid] at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
[MonoDroid] at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
[MonoDroid] at android.view.View.dispatchPointerEvent(View.java:7886)
[libc] Fatal signal 6 (SIGABRT) at 0x00000838 (code=-6), thread 2104 (pXForms.Android)

Posts

  • laygrlaygr MXMember, University

    My current solution is to drop all the table sections, create & add new ones with the new data (is it the only way?)

    Xamarin.Forms.Device.BeginInvokeOnMainThread (() =>
            {
                var newCitiesSection = new TableSection ();
                var newTerminalsSection = new TableSection ();
    
                newCitiesSection.Add (PlacesVM.Cities.ConvertAll<TextCell> ((c) => new TextCell{ Text = c.Name }));
    
                newTerminalsSection.Add (PlacesVM.Terminals.ConvertAll<TextCell> ((t) => new TextCell {
                    Text = t.Name,
                    Detail = t.City
                }));
    
                ResultsTable.Root.Clear ();
                ResultsTable.Root.Add (new List<TableSection>{ newCitiesSection, newTerminalsSection });
    
            });
    
  • MarkWallisMarkWallis AUMember

    I get the same problem.

    Make sure your adapter calls notifyDataSetChanged()

    I can't find NotifyDataSetChanged. Is it exposed anywhere Xamarin?
    Have tried inserting a new row as well as clearing the TableSection and rebuilding, am definitely doing this on the UI thread.
    What is the correct way to do this?

  • laygrlaygr MXMember, University

    I started using ViewModels. In my view model I notify the change:

    member this.onResultsChanged(_) =
        propertyChanged.Trigger(this, new  PropertyChangedEventArgs("Results"))
    // this is F#, by the way
    

    And in my Xaml I'm just binding to the "Results" property and it all works fine.

    <ListView
        x:Name="ResultsLV"
        ItemsSource = "{Binding Results}"
        ...
        >
        ...
    </ListView>
    

    But I think that internally, the list view is doing a complete reload whenever "Results" changes.

  • GeoffArmstrongGeoffArmstrong CAMember ✭✭

    Well, of course. You're reassigning the ItemsSource property.

  • laygrlaygr MXMember, University

    Yes... the question remains: What's the point of an observable collection if the list would be doing complete reloading.

  • laygrlaygr MXMember, University

    Maybe it was a bug, maybe I was doing it wrong, but now is working. You can see de rows smoothly disappearing on delete.

Sign In or Register to comment.