Android Question Clearing a xCustomListView with lazy loading make the app crash

Martin Larsen

Active Member
Licensed User
Longtime User
VisibleRangeChanged is a godsend for my project as I was facing out of memory issues with my xCLV.

However, there is a problem as calling CLV.clear makes the app crash with the following exception:

B4X:
customlistview_findindexfromoffset (B4A line: 370)
Dim CurrentItem As CLVItem = items.Get(Position)
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
    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._findindexfromoffset(customlistview.java:475)
    at b4a.example3.customlistview._getfirstvisibleindex(customlistview.java:585)
    at b4a.example3.customlistview._updatevisiblerange(customlistview.java:1620)
    at b4a.example3.customlistview._scrollhandler(customlistview.java:1444)
    at b4a.example3.customlistview._sv_scrollchanged(customlistview.java:1596)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.BA$1.run(BA.java:325)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)

It has nothing to do with VisibleRangeChanged as this never called after clearing the CLV. The app crashes as soon as CLV.Clear is called.

Note: The problem only occurs if the CLV has been scrolled before called Clear.

I have attached an example based on the original demo code.
 

Attachments

  • xCustomListView_with_clear.zip
    17.8 KB · Views: 391

DonManfred

Expert
Licensed User
Longtime User
Again here.

B4X:
'Clears all items.
Public Sub Clear
    items.Clear
    sv.ScrollViewInnerPanel.RemoveAllViews
    SetScrollViewContentSize(0)
End Sub

This is the code from xcustomlistview class. It does NOT access item 0 from any list like in your exception. The error happens somewhere else

customlistview_findindexfromoffset (B4A line: 370)
Dim CurrentItem As CLVItem = items.Get(Position)
java.lang.IndexOutOfBoundsException: Invalid index
0, size is 0
Based on the error it happens in the line
B4X:
Dim CurrentItem As CLVItem = items.Get(Position)

ETA: The code does not exists in clv Class...
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
Thanks for your assistance, I really appreciate it!

B4A line: 370

Check the generated java source to find out which line exactly raises the error....

I am fully aware that the error takes place at line 370 of CustomListView.bas (I always click on the exception to go to the source code).

But what I mean is that this file is the library from Erel's orginial post introducing xCustomListView: https://www.b4x.com/android/forum/t...listview-cross-platform-customlistview.84501/

I use the original library and the original example, only modified as per Erel's tutorial as adviced here.

ETA: The code does not exists in clv Class...

Sorry, I don't know what ETA means except estimated time of arrival which I guess is not the meaning here :)
 
Upvote 0

Martin Larsen

Active Member
Licensed User
Longtime User
I have found that sometimes debugging is easier with the legacy debugger as it shows you a call stack at the time of the exception instead of just stopping.

The problem only occurs if the topmost index is not zero. Only then the following is triggered from the sv in line 468 of CustomListView.bas:

B4X:
Private Sub sv_ScrollChanged(Position As Int)
    ScrollHandler
End Sub

which in turns calls ScrollHandler and then UpdateVisibleRange. Modifying UpdateVisibleRange in line 254 fixes the problem:

B4X:
Private Sub UpdateVisibleRange
    If MonitorVisibleRange = False Or items.Size = 0 Then Return ' if there are no items, do nothing!

Erel, please check if this is a correct fix of the problem in the library or it should be handled somewhere else.
 
Upvote 0
Top