Android Question how to properly delete item from CLV in Loop ?

Addo

Well-Known Member
Licensed User
i am trying to delete some clv items based on some if condition by doing the following


B4X:
For c = 0 To CLVLIST.Size - 1
Dim p As B4XView = CLVLIST.GetPanel(c)
If p.GetView(5).Text = "state" Then
CLVLIST.RemoveAt(c)
End If

Next

i get the following error


** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv5 (java line: 2414)
java.lang.IndexOutOfBoundsException: Invalid index 21, size is 21
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
at b4a.example3.customlistview._getrawlistitem(customlistview.java:435)
at b4a.example3.customlistview._getpanel(customlistview.java:428)
at app.com.main._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv5(main.java:2414)
at app.com.main$ResumableSub_NewData.resume(main.java:1861)
at app.com.main._vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv3(main.java:1695)
at app.com.main._astream_newtext(main.java:1074)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1154)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
java.lang.IndexOutOfBoundsException: Invalid index 21, size is 21
** Activity (main) Pause, UserClosed = false **
 

mangojack

Well-Known Member
Licensed User
Longtime User
You are reducing the size of CLV as you remove items ... you must loop in reverse.

something like

B4X:
 For c = CLV.Size - 1 To 1 Step -1
       CLV.RemoveAt(c)
 
Upvote 0
Top