访问更新的列表视图的第一个元素会导致System.ArgumentOutOfRangeException

时间:2019-07-20 10:25:03

标签: android listview user-interface xamarin adapter

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

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

2 个答案:

答案 0 :(得分:0)

您可以一次删除所有项目。发送位置以删除事件

img_remove.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(eventsToRemove.size() > 0)
              events.Remove(eventsToRemove[position])
        }
    });

答案 1 :(得分:0)

这个问题

  

此外,当我单击更新列表的第一个元素时,   在控制台上跟随输出

[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;
    }

注意:如您所说,我无法重现第一个问题。