Android Question [SOLVED]Invalid index when populating listview

Tyler

Member
Licensed User
Longtime User
Hey, been staring at this for hours and can't figure out why I'm getting an invalid index each time. Here's my code:

B4X:
Private searchbox As EditText
    
    Private searchlist As ListView
    Dim lst As List


Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("main")
    lst.Initialize
    lst.AddAll(Array As String("TEST 1", "TEST2", "TEST3", "TEST4", "TEST 5", "TEST6", "TEST7", "TEST8", "TEST 9", "TEST10", "TEST11", "TEST12"))
  

End Sub


Sub searchbox_EnterPressed
Dim lstget As String
For i = 0 To lst.Size - 1
    lstget = lst.Get(i)
If lstget.Contains(searchbox.Text) == False Then
    lst.RemoveAt(i)
End If
Next
searchlist.Clear
Dim sb As String
For k = 0 To lst.Size - 1
    sb = lst.Get(k)
    searchlist.AddSingleLine(sb)
Next
searchbox.Visible = True
    searchlist.Visible = True
End Sub




and here's the full error log I'm getting:



B4X:
Error occurred on line: 482 (main)
java.lang.IndexOutOfBoundsException: Invalid index 6, size is 6
    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 java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:161)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:157)
    at anywheresoftware.b4a.objects.EditTextWrapper$2.onEditorAction(EditTextWrapper.java:107)
    at android.widget.TextView.onEditorAction(TextView.java:4770)
    at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:139)
    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:304)
    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:78)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5461)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)



Any insight would be much appreciated, thanks!
 

klaus

Expert
Licensed User
Longtime User
I'm afraid that this si wrong:
B4X:
For i = 0 To lst.Size - 1
    lstget = lst.Get(i)
    If lstget.Contains(searchbox.Text) == False Then
        lst.RemoveAt(i)
    End If
Next

Try this:
B4X:
For i = lst.Size - 1 To 0 Step -1
    lstget = lst.Get(i)
    If lstget.Contains(searchbox.Text) = False Then
        lst.RemoveAt(i)
    End If
Next
 
Upvote 0

Tyler

Member
Licensed User
Longtime User
You're godlike, thanks :) Any chance you wouldn't mind explaining what that does? I'm unable to wrap my head around it for some reason.
 
Upvote 0
Top