Forum Xamarin Cross Platform with Xamarin

Listview with grouping :System.ArgumentOutOfRangeException: Index was out of range

Hello,
I'm starting xamarin and i'm facing a binding problem with grouping in listview.
Here is my content page:

       RootMenuViewModel rootUserProfileViewModel;
        public RootMenuPage(RootPage rootPage)
        {            
            BindingContext = rootUserProfileViewModel = new RootMenuViewModel(this);          
            LoadUI();                        
        }
       private void LoadUI()
        {
            Title = "menu";
            listView = new ListView
            {
                HasUnevenRows=false,
                BindingContext = rootUserProfileViewModel,
                IsGroupingEnabled= true,
                GroupDisplayBinding = new Binding("GroupName"),                
                ItemTemplate = new DataTemplate(() => {                    
                    TextCell c = new TextCell();
                    c.SetBinding(TextCell.TextProperty, "Label");
                    return c;
                }),                             
            };
            listView.SetBinding(ListView.ItemsSourceProperty, "GroupedClassif");
          .......
          ........
          Content = stack;
       }

      protected override async void OnAppearing()
        {
            base.OnAppearing();
            await rootUserProfileViewModel.LoadDataAsync();            
        }

Here is my viewModel:

     class RootMenuViewModel : BaseViewModel
    {
      public ObservableCollection<ItemClassification> Classif { get; set; }
      public ObservableCollection<ItemClassificationGroups> GroupedClassif { get; set; }

       public RootMenuViewModel(Page page)
            : base(page)
        {
            GroupedClassif = new ObservableCollection<ItemClassificationGroups>();
        }

       public async Task LoadDataAsync()
        {
           //here i gte the data and create groups(GroupedClassif )
        }


        public class ItemClassificationGroups: ObservableCollection<ItemClassification>
        {
            public string GroupName { get;set; }
            public ItemClassificationGroups(string groupName)
            {
                this.GroupName = groupName;
            }
     }           
    }

And here the stack trace:

02-14 02:20:49.575 I/MonoDroid( 5525): UNHANDLED EXCEPTION:
02-14 02:20:49.576 I/MonoDroid( 5525): System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
02-14 02:20:49.576 I/MonoDroid( 5525): Parameter name: index
02-14 02:20:49.576 I/MonoDroid( 5525): at (wrapper dynamic-method) System.Object:15668690-48b8-44a1-8133-8e6fa3863864 (intptr,intptr)
02-14 02:20:49.576 I/MonoDroid( 5525): at (wrapper managed-to-native) Java.Interop.NativeMethods:java_interop_jnienv_call_nonvirtual_void_method_a (intptr,intptr&,intptr,intptr,intptr,Java.Interop.JniArgumentValue)
02-14 02:20:49.576 I/MonoDroid( 5525): at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue
args) [0x00073] in :0
02-14 02:20:49.576 I/MonoDroid( 5525): at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0005d] in :0
02-14 02:20:49.576 I/MonoDroid( 5525): at Android.Widget.BaseAdapter.NotifyDataSetChanged () [0x0000a] in :0
02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.Platform.Android.ListViewAdapter.OnDataChanged () [0x00034] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:563
02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.Platform.Android.ListViewAdapter.OnCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:553
02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in D:\agent\_work\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:771 02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnCollectionChangedGrouped (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x000d5] in D:\agent_work\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:798
02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, System.Boolean fixWindows) [0x00008] in D:\agent\_work\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:975 02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.Internals.TemplatedItemsList2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00000] in D:\agent_work\1\s\Xamarin.Forms.Core\TemplatedItemsList.cs:968
02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.ListProxy.OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000a] in D:\agent_work\1\s\Xamarin.Forms.Core\ListProxy.cs:233
02-14 02:20:49.576 I/MonoDroid( 5525): at Xamarin.Forms.ListProxy+<>c__DisplayClass33_0.b__0 () [0x00018] in D:\agent_work\1\s\Xamarin.Forms.Core\ListProxy.cs:206
02-14 02:20:49.576 I/MonoDroid( 5525): at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in :0
02-14 02:20:49.576 I/MonoDroid( 5525): at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in :0
02-14 02:20:49.576 I/MonoDroid( 5525): at (wrapper dynamic-method) System.Object:63f309a2-31f8-4f88-a5a7-04f6212f2c3d (intptr,intptr)

Best Answer

  • SLASLA Member ✭✭
    Accepted Answer

    Sorry for posting same many threads..
    Solved by using Device.BeginInvokeOnMainThread(() => { });

Answers

  • SLASLA Member ✭✭
    Accepted Answer

    Sorry for posting same many threads..
    Solved by using Device.BeginInvokeOnMainThread(() => { });

Sign In or Register to comment.