Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] [Android] ArgumentOutOfRangeException in CollectionView: Adapter miscalculates item position when using CV Header #7243

Closed
janschoelchAtOK opened this issue Aug 22, 2019 · 4 comments · Fixed by #7199
Labels
Projects

Comments

@janschoelchAtOK
Copy link

janschoelchAtOK commented Aug 22, 2019

Description

When using the CollectionView in Xamarin.Forms 4.2 and using the new CV Header feature, the SelectableItemsViewAdapter seems to miscalculate the item positions.
This leads to unwanted behaviour (wrong item selections) and also to exceptions.

The issue exists since CollectionView Headers were introduced (so much love for this! :-)) for Android (which is XF4.2-pre1 I think).

As things run fine on iOS, I could not reproduce the error there. This is Android only.

I faced and reproduced this issue on an Huawei P Smart+ (2018) running Android 8.1. But a quick test on several Emulators gave me the same exception - so this is neither device-specific nor dependent on the OS version.

Steps to Reproduce

  1. Add a simple CollectionView to a XF Project running XF4.2
  2. Make CollectionView Selectable (SelectionMode="Single")
  3. Add a Header to the CollectionView
  4. Run your app and select any item --> The selected item is not correct.

OR (easier)

  1. Start the attached Solution
  2. Select any item

Expected Behavior (in attached repo)

  • The dialog that appears should show the correct selection

Actual Behavior (in attached repo)

  • The dialog shows the [SelectedItem + 1]
  • When selecting the last item in the CollectionView ("I am teal"), the application crashes with an IndexOutOfRangeException as there is no [SelectedItem + 1].

StackTrace:

08-22 12:34:27.914 I/MonoDroid( 8272): UNHANDLED EXCEPTION: 08-22 12:34:27.973 I/MonoDroid( 8272): System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. 08-22 12:34:27.973 I/MonoDroid( 8272): Parameter name: index 08-22 12:34:27.973 I/MonoDroid( 8272): at System.Collections.Generic.List'1[T].get_Item (System.Int32 index) [0x00009] in <d4a23bbd2f544c30a48c44dd622ce09f>:0 08-22 12:34:27.973 I/MonoDroid( 8272): at Xamarin.Forms.Platform.Android.SelectableItemsViewAdapter.UpdateFormsSelection (System.Int32 adapterPosition) [0x00020] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\SelectableItemsViewAdapter.cs:138 08-22 12:34:27.973 I/MonoDroid( 8272): at Xamarin.Forms.Platform.Android.SelectableItemsViewAdapter.SelectableClicked (System.Object sender, System.Int32 adapterPosition) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\SelectableItemsViewAdapter.cs:125 08-22 12:34:27.973 I/MonoDroid( 8272): at Xamarin.Forms.Platform.Android.SelectableViewHolder.OnViewHolderClicked (System.Int32 adapterPosition) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\SelectableViewHolder.cs:51 08-22 12:34:27.973 I/MonoDroid( 8272): at Xamarin.Forms.Platform.Android.SelectableViewHolder.OnClick (Android.Views.View view) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\SelectableViewHolder.cs:40 08-22 12:34:27.973 I/MonoDroid( 8272): at Android.Views.View+IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_v) [0x00011] in <b781ed64f1d743e7881ac038e0fbdf85>:0 08-22 12:34:27.973 I/MonoDroid( 8272): at (wrapper dynamic-method) System.Object.43(intptr,intptr,intptr)

Basic Information

  • Version with issue: Xamarin.Forms 4.2
  • Last known good version: None, as the issue exists since the first Version where CV headers were availabe for Android
  • IDE: Visual Studio Professional 2017
  • Platform Target Frameworks:
    • iOS: 12.1 (runs without Errors)
    • Android: 8.1
    • UWP: not tested
  • Android Support Library Version: 28.0.01
  • Nuget Packages: Xamarin.Forms 4.2-RC1 (4.2.0.709249)
  • Affected Devices: Tablets and Phones

Screenshots

No visual error detected

Reproduction Link

CVHeaderDemo.zip

@janschoelchAtOK janschoelchAtOK added s/unverified New report that has yet to be verified t/bug 🐛 labels Aug 22, 2019
@pauldipietro pauldipietro added this to New in Triage Aug 22, 2019
@janschoelchAtOK janschoelchAtOK changed the title [Bug] [Android] IndexOutOfRangeException in CollectionView: Adapter miscalculates item position when using CV Header [Bug] [Android] ArgumentOutOfRangeException in CollectionView: Adapter miscalculates item position when using CV Header Aug 22, 2019
@janschoelchAtOK
Copy link
Author

I just saw the changes that @hartez did in #7199, especially in the most recent commit.
As this issue is propably connected to #7121 and #7102, there is a chance that this may already be fixed by @hartez' PR

@hartez
Copy link
Contributor

hartez commented Aug 22, 2019

I just saw the changes that @hartez did in #7199, especially in the most recent commit.
As this issue is propably connected to #7121 and #7102, there is a chance that this may already be fixed by @hartez' PR

Just confirmed this with a build from PR #7199 - that PR does fix this issue.

@samhouts samhouts added this to In Progress in v4.3.0 Aug 23, 2019
@samhouts samhouts removed this from New in Triage Aug 23, 2019
@samhouts samhouts added this to Backlog in CollectionView Aug 27, 2019
v4.3.0 automation moved this from In Progress to Done Aug 28, 2019
CollectionView automation moved this from Backlog to Done Aug 28, 2019
samhouts pushed a commit that referenced this issue Aug 28, 2019
* Move all the header/footer adjustment to IItemsViewSource
fixes #7121
fixes #7102
partially implements #3172
fixes #7243

* Fix selection bugs introduced by header/footer on Android

* Implement grouping for CollectionView on Android

* Enable grouping tests for Android

* Naming and comment cleanup

* Update Xamarin.Forms.Platform.Android/CollectionView/ListSource.cs

Co-Authored-By: Gerald Versluis <gerald.versluis@microsoft.com>

* Update Xamarin.Forms.Platform.Android/CollectionView/ObservableGroupedSource.cs
@samhouts samhouts added this to In Progress in v4.4.0 Aug 30, 2019
@samhouts samhouts moved this from In Progress to Done in v4.4.0 Aug 30, 2019
@samhouts samhouts removed this from Done in v4.3.0 Sep 3, 2019
@samhouts samhouts removed this from Done in v4.4.0 Sep 11, 2019
@samhouts samhouts added this to In Progress in v4.3.0 Sep 11, 2019
@samhouts samhouts moved this from In Progress to Done in v4.3.0 Sep 11, 2019
felipebaltazar pushed a commit to felipebaltazar/Xamarin.Forms that referenced this issue Oct 16, 2019
* Move all the header/footer adjustment to IItemsViewSource
fixes xamarin#7121
fixes xamarin#7102
partially implements xamarin#3172
fixes xamarin#7243

* Fix selection bugs introduced by header/footer on Android

* Implement grouping for CollectionView on Android

* Enable grouping tests for Android

* Naming and comment cleanup

* Update Xamarin.Forms.Platform.Android/CollectionView/ListSource.cs

Co-Authored-By: Gerald Versluis <gerald.versluis@microsoft.com>

* Update Xamarin.Forms.Platform.Android/CollectionView/ObservableGroupedSource.cs
felipebaltazar pushed a commit to felipebaltazar/Xamarin.Forms that referenced this issue Oct 16, 2019
* Move all the header/footer adjustment to IItemsViewSource
fixes xamarin#7121
fixes xamarin#7102
partially implements xamarin#3172
fixes xamarin#7243

* Fix selection bugs introduced by header/footer on Android

* Implement grouping for CollectionView on Android

* Enable grouping tests for Android

* Naming and comment cleanup

* Update Xamarin.Forms.Platform.Android/CollectionView/ListSource.cs

Co-Authored-By: Gerald Versluis <gerald.versluis@microsoft.com>

* Update Xamarin.Forms.Platform.Android/CollectionView/ObservableGroupedSource.cs
@samhouts samhouts removed this from Done in CollectionView May 6, 2020
@Toomas75
Copy link

It's back MAUI 8.0.6 , Android 8.1
crash log:
System.ArgumentOutOfRangeException
System.ArgumentOutOfRangeException: ArgumentOutOfRange_IndexMustBeLess Arg_ParamName_Name, index

System.Collections.Generic.List1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].get_Item(Int32 ) System.Collections.Generic.List1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].System.Collections.IList.get_Item(Int32 )
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.ElementAt(Int32 )
Microsoft.Maui.Controls.Handlers.Items.ObservableItemsSource.GetItem(Int32 )
Microsoft.Maui.Controls.Handlers.Items.UngroupedItemsSource.GetItem(Int32 )
Microsoft.Maui.Controls.Handlers.Items.SelectableItemsViewAdapter2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.IGroupableItemsViewSource, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].UpdateMauiSelection(Int32 ) Microsoft.Maui.Controls.Handlers.Items.SelectableItemsViewAdapter2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Controls.Handlers.Items.IGroupableItemsViewSource, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SelectableClicked(Object sender, Int32 adapterPosition)
Microsoft.Maui.Controls.Handlers.Items.SelectableViewHolder.OnViewHolderClicked(Int32 )
Microsoft.Maui.Controls.Handlers.Items.SelectableViewHolder.OnClick(View )
Android.Views.View.IOnClickListenerInvoker.n_OnClick_Landroid_view_View_(IntPtr jnienv, IntPtr native__this, IntPtr native_v)
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPL_V(_JniMarshal_PPL_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0)

@alexsmi-noveo
Copy link

Confirm, the same for me: MAUI 8.0.20:

System.Collections.Generic.List1[[Pootsy.Mobile.Models.Bookings.BookingListItem, Pootsy.Mobile, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_Item(Int32 ) System.Collections.Generic.List1[[Pootsy.Mobile.Models.Bookings.BookingListItem, Pootsy.Mobile, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].System.Collections.IList.get_Item(Int32 )
Microsoft.Maui.Controls.Handlers.Items.ListSource.GetItem(Int32 position)
AndroidX.RecyclerView.Widget.RecyclerView.Adapter.n_OnBindViewHolder_Landroidx_recyclerview_widget_RecyclerView_ViewHolder_I(IntPtr jnienv, IntPtr native__this, IntPtr native_holder, Int32 position)
Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PPLI_V(_JniMarshal_PPLI_V callback, IntPtr jnienv, IntPtr klazz, IntPtr p0, Int32 p1)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
No open projects
v4.3.0
  
Done
Development

Successfully merging a pull request may close this issue.

5 participants