Arrays, Lists, and File.ReadList

Roger Garstang

Well-Known Member
Licensed User
Longtime User
All the conversion between these are driving me nuts. In a code module I had no problem, but in a library they are giving me issues. I have a library with a custom label (I'm Making my own Combobox/Spinner) holding an Item list (A B4A List to make it simplest). I always make sure the List is initialized even if empty. If I pass in a Null List it handles it properly and if I pass an Array as String() it works too. All my error checking checks for List being passed in to be initialized and when I show the List it checks size of the list or doesn't show it.

The problem seems to be with File.ReadList and possibly variable scope or something. Currently I fill the list in the library with Initialize2 in the List. I have log messages where I output the size of the passed in list- My file read with File.ReadList is US States and it reports 50. When the label is tapped it shows the list: My Array as String() shows its list, my Null List shows no list, and my File.ReadList Force Closes the application.

It is like it sees the list size still, but there is no list. I always thought that even when a local is out of scope it still exists since something else is referencing it. Behind the scenes the Array as String() is making a List and it remains and works fine. The documentation says that initialize2 shares lists, so I tried doing a Clear and AddAll then, but not only did it still fail with the File.ReadList it also now failed with the Array as String(). So I guess the Array doesn't convert right that way.

Is there something File.ReadList does when going out of Scope in B4A that handles the List it created differently? I used this same way in a Code Module and it works fine, but it is like B4A isn't seeing the List being used by the library and killing it.
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
The forum was down or something yesterday and I couldn't post my findings (Navigating to pages loaded them about 1/3 of the way then hung...looked like it was having issues polling database)

I think I fixed it, but I'm not sure what the difference is. Templates and Interface type stuff in C++ based languages was never my strong point. I discovered the List was storing fine, but the part that was crashing was in my library when I passed an array of the List to the Alert Dialog to show. I was doing a .getObject().toArray() on the list and casting it to a String or CharSequence array. This worked fine for the B4A Array as String() parameters I was passing, but failed for the File.ReadList parameters. I got to reading the documentation for the other variation of toArray and tried getObject().toArray(new String[0]) on the List and passed it to the dialog. This now appears to be working, but to me seems like the same thing I was doing. The "new String[0]" stuff is new to me and the "0" I'm kinda curious about as to whether if other numbers are passed it would return from that part of the array on or something.

In the end it is working though and I have a nice Custom Combobox/Spinner. I'm a little disappointed at how Android handles SetSelection for the List of the dialog in Touch Mode though. I wanted to use the list without the Radio/Option Buttons, but SetSelection didn't appear to actually select a value or even scroll to it (I was fine without a highlight, but couldn't even get that). So, I switched to the Radio/Option button variety that does both, and much simpler by just passing the selection to the same method used to pass the list.
 
Upvote 0
Top