Android Question [PreoptimizedCLV] "Type does not match" (when Panel is not used) and "IndexOutOfBoundsException" (when Wait For is used in VisibleRangeChanged)

Hi

I have been having problems using PreoptimizedCLV when the list exceeds a certain length. To debug it, I created a simple project that loads a list of labels. Immediately, there was a crash that I was not able to understand and fix, right after I scroll down past a certain length.

The error message is as follows:
Error:
preoptimizedclv_visiblerangechanged (java line: 486)
java.lang.RuntimeException: Type does not match (class android.widget.TextView)
    at anywheresoftware.b4a.objects.B4XViewWrapper.typeDoesNotMatch(B4XViewWrapper.java:387)
    at anywheresoftware.b4a.objects.B4XViewWrapper.asPanelWrapper(B4XViewWrapper.java:108)
    at anywheresoftware.b4a.objects.B4XViewWrapper.RemoveAllViews(B4XViewWrapper.java:329)
    at b4a.example.testpclv.preoptimizedclv._visiblerangechanged(preoptimizedclv.java:486)
    at b4a.example.testpclv.main._list_visiblerangechanged(main.java:391)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
    at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1178)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7830)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1040)

Here is a Google Drive link to the sample project (zip):
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Use File - Export as zip.
2. This is a very very short list. Long list = 30,000+ items.

You are expected to add a panel to item.Panel.AddView

 
Upvote 0
1. Use File - Export as zip.
2. This is a very very short list. Long list = 30,000+ items.

You are expected to add a panel to item.Panel.AddView



Thank you @Erel , adding the Panel worked.

Now I have upgraded it to use a dummy custom view, which is closer to what I have built for my work.

The one I built for my work crashes immediately upon loading, when the list is only 9 items long. This one crashes when you scroll down too quickly.

The crash output is as follows:
Crash Output:
FirstIndex = 7, LastIndex = 17
20 of 65
main$ResumableSub_list_VisibleRangeChangedresume (java line: 433)
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.get(ArrayList.java:437)
    at anywheresoftware.b4a.objects.collections.List.Get(List.java:117)
    at b4a.example.testpclv.main$ResumableSub_list_VisibleRangeChanged.resume(main.java:433)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1760)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7830)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1040)

Looks like the size of the list became 1 as I was scrolling.

I suspect it could be something to do with threading as I have used "Sleep(0)" commands in the custom view.
 

Attachments

  • TestPreoptimizedCLV.zip
    13.4 KB · Views: 167
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I suspect it could be something to do with threading as I have used "Sleep(0)" commands in the custom view.
Sleep has nothing to do with threading.
Please make it simpler to help you and post the relevant code directly.

Watch the resumable subs video tutorial: https://www.b4x.com/etp.html
It is critical to understand how it works.

You cannot use Wait For inside this sub (at least not without checking for reentrances).
 
Upvote 0
Sleep has nothing to do with threading.
Please make it simpler to help you and post the relevant code directly.

Watch the resumable subs video tutorial: https://www.b4x.com/etp.html
It is critical to understand how it works.

You cannot use Wait For inside this sub (at least not without checking for reentrances).


Thank you @Erel . I removed the Wait For and it no longer crashed. I now understand after watching the video more specifically how Resumable Subs work, by causing execution to go somewhere else and re-enter.

My use case is I need that sub to calculate the height so that I can resize my rows for the cases where my custom views can be of varying height due to the content (such as text), and to do that I need to Sleep(0) after adding the custom view into the panel so that it can be measured. This makes the sub into a resumable, and cause glitches such as crashing.

Is it possible to do this without using a resumble sub? Or, how may I use it properly (and check for reentrances, like you said?) Thank you!
 
Upvote 0
None of this will work.
I tried something that did work:

Used an Event Sub instead of the ResumableSub's Complete event:
Sub list_Item_HeightChanged (Position As Int, NewHeight As Int)
    list.ResizeItem(Position, NewHeight)
End Sub

This eliminated the need to use Wait For inside VisibleRangeChanged.

The real mistake is that you are making things more complicated than they should be. Don't use PreoptimizedCLV unless you want to show many many items (10k+).

Start simple, without using lazy loading at all.

I understand that you are trying to simplify the problem for me. However, what I am coding is for production purposes and the list may indeed exceed 10k+. I did not decide to use PreoptimizedCLV as default and I have taken many considerations before doing so, so at this point some things have to be that complicated.

Thank you so much for helping me find this solution!
 
Last edited:
Upvote 0
You must know and set the height of each items when you configure PCLV. It is not possible to later change it (not unless you learn how PCLV works and make the requires changes).

Thank you for this warning and your timely support on the problem I faced, and also for creating PCLV, and not to mention the whole B4X system.

I really appreciate the different perspective you gave me and I know where to come to when I face problems again.
 
Upvote 0
Top