android - 访问更新 ListView 的第一个元素会导致 System.ArgumentOutOfRangeException

标签 android listview user-interface xamarin adapter

我更新列表,从中删除不需要的项目,然后调用 adapter.NotifyDataSetChanged。当我单击更新后的 ListView 中的第一个元素 时,System.ArgumentOutOfRangeException 弹出(似乎该元素保留了它之前在列表中的索引已更新)。相反,当我点击更新后的 ListView 中的其他项目时,没有任何问题。

List<Event> eventsToRemove = new List<Events>;

//Populating eventsToRemove with some "not-matching-some-criterion" <Event> instances taken from the original List<Event> events

for (var i = 0; i < eventsToRemove.Count; i++) {
                        events.Remove(eventsToRemove[i]);
}
adapter.NotifyDataSetChanged();

我猜问题出在更新适配器的线程和管理 UI 的线程上。我想解决这个问题:

Unhandled Exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

日志:

07-20 12:54:41.252 D/ViewRootImpl@3e1ee4d[Toast](18915): MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
07-20 12:54:41.667 D/ViewRootImpl@3e1ee4d[Toast](18915): mHardwareRenderer.destroy()#4
07-20 12:54:41.667 D/ViewRootImpl@5f20f01[MainActivity](18915): ViewPostImeInputStage processPointer 0
07-20 12:54:41.668 D/ViewRootImpl@3e1ee4d[Toast](18915): dispatchDetachedFromWindow
07-20 12:54:41.674 D/InputTransport(18915): Input channel destroyed: fd=75
07-20 12:54:41.716 D/ViewRootImpl@5f20f01[MainActivity](18915): ViewPostImeInputStage processPointer 1
07-20 12:54:42.057 D/Mono    (18915): Assembly Ref addref Mono.Android[0x73a4bea880] -> System.Runtime.Serialization[0x738f4cf780]: 3
Unhandled Exception:

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Thread finished: <Thread Pool> #10
Il thread 0xa è terminato con il codice 0 (0x0).
Thread finished: <Thread Pool> #5
Il thread 0x5 è terminato con il codice 0 (0x0).
Thread finished: <Thread Pool> #12
Il thread 0xc è terminato con il codice 0 (0x0).
Thread finished: <Thread Pool> #6
Il thread 0x6 è terminato con il codice 0 (0x0).
Thread finished: <Thread Pool> #11
Il thread 0xb è terminato con il codice 0 (0x0).

Github 链接与各自的代码:https://github.com/eddybudge/PerugiaEventi1/blob/master/PerugiaEventi1/MainActivity.cs

另外,当我点击更新列表的第一个元素时,我在控制台上得到以下输出:

[0:] Position: 0 Number of elements inside the list: 9
[0:] Position: 1 Number of elements inside the list: 9
[0:] Position: 2 Number of elements inside the list: 9
[0:] Position: 3 Number of elements inside the list: 9
[0:] Position: 4 Number of elements inside the list: 9
[0:] Position: 5 Number of elements inside the list: 9
[0:] Position: 6 Number of elements inside the list: 9
[0:] Position: 7 Number of elements inside the list: 9
[0:] Position: 8 Number of elements inside the list: 9
[0:] Position: 9 Number of elements inside the list: 9

,而当我点击任何其他按钮时,我得到

[0:] Position: x Number of elements inside the list: y
  • 很奇怪,应该只打印一行!!!

(我使用了 System.Diagnostics.Debug.WriteLine("Position: "+position+"Number of elements inside the list: "+eventi.Count()); 每次点击一个按钮)

更新:

最后我决定捕获异常 并使用内部捕获0 作为索引 - 因为在测试期间它总是第一个导致问题的元素。 顺便说一句,我没有触及问题的核心 - 这就是造成问题的原因和原因。

Original list

Selecting a date

The updated list

最佳答案

对于这个问题

Also, when I click on the first element of the updated list, I get the following output on the console

[0:] Position: 0 Number of elements inside the list: 9
[0:] Position: 1 Number of elements inside the list: 9
[0:] Position: 2 Number of elements inside the list: 9
[0:] Position: 3 Number of elements inside the list: 9
[0:] Position: 4 Number of elements inside the list: 9
[0:] Position: 5 Number of elements inside the list: 9
[0:] Position: 6 Number of elements inside the list: 9
[0:] Position: 7 Number of elements inside the list: 9
[0:] Position: 8 Number of elements inside the list: 9
[0:] Position: 9 Number of elements inside the list: 9

你只需要添加下面的代码

 if (!bottoneEvento.HasOnClickListeners) {
 }

如下:

public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.list_item, parent, false);
        var bottoneEvento = view.FindViewById<Button>(Resource.Id.bottoneEventAdapter);

        bottoneEvento.Text = eventi[position].Titolo;
        //bottoneEvento.Tag = position;  --forse non serve
        if (!bottoneEvento.HasOnClickListeners)  // key code
        {
            bottoneEvento.Click += (sender, args) =>
          {
            System.Diagnostics.Debug.WriteLine("Position: "+position+" Number of elements inside the list: "+eventi.Count());
            //bisogna, immagino fare l'update della view con notify - prima creando un observer.
            Intent dettagli = new Intent(Application.Context, typeof(EventoInDettaglio));
            dettagli.PutExtra("titolo", bottoneEvento.Text);
            dettagli.PutExtra("url", eventi[position].Url);
            dettagli.PutExtra("inizia", eventi[position].Inizio);
            dettagli.PutExtra("finisce", eventi[position].Fine);
            dettagli.PutExtra("descrizione", eventi[position].Descrizione);
            Application.Context.StartActivity(dettagli);
          };
        }

        return view;
    }

注意:我无法重现您上面所说的第一个问题。

关于android - 访问更新 ListView 的第一个元素会导致 System.ArgumentOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57123819/

相关文章:

php - 安卓 : send json data to the Server

android - 在方法中我的列表有元素,但是当我出来时列表是空的

listview - Xamarin 窗体 FFImageLoading ListView 控件

Javafx 组合框重置问题

安卓自定义键盘 : change candidate height

android - 如何在 Spinner Android 中添加项目

Android 文本对齐 - Pixel Perfect

android - 不同设备上的listview空指针异常

java - 不确定为什么会抛出 NullPointer

python - 为 python 应用程序创建 Web 界面的最佳策略是什么?