Android Question list from a list

67biscuits

Member
What's wrong:
Private Sub spnSection_ItemClick (Position As Int, Value As Object)
    '(frame, elect, eng, brakes, FACI,suspension, wheels, custom)
Log(Position & Value)
    Dim lst As List = allSections.Get(Position)
    Log(lst)
    spnSubSection.AddAll(lst)

End Sub

why does this fail when it hits the spnSubSection.addall line?
if I run it from B4XPage_Created using a static value (eg; frame, elect, eng...etc) then it's fine.

Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
3Brakes
(List) (ArrayList) [Controls, Cables/Lines, Pads/Shoes, Resevoir]
** Activity (main) Pause event (activity is not paused). **
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create (first time) **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
3Brakes
(List) (ArrayList) [Controls, Cables/Lines, Pads/Shoes, Resevoir]
Error occurred on line: 106 (B4XMainPage)
java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.List cannot be cast to java.util.Collection
at anywheresoftware.b4a.objects.SpinnerWrapper.AddAll(SpinnerWrapper.java:121)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
at anywheresoftware.b4a.BA$1.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8261)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:622)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:977)
 

67biscuits

Member
adding the lists:
    Dim sections As List = Array As String("Frame", "Engine", "Exhaust", "Fuel", "Brakes", "Wheels", "Suspension", "Electrical", "Custom", "Maintenance")
    Dim frame As List = Array As String("Rails", "Panels", "Fenders")
    Dim engine As List = Array As String("Heads", "Cylinders", "Transmission")
    Dim exhaust As List = Array As String("Pipe/s", "Muffler/s", "Other")
    Dim fuel As List = Array As String("Tank", "Valve", "Carb/s", "Other")
    Dim brakes As List = Array As String("Front", "Rear", "Cables", "Resevoir", "Lines", "ABS")
    Dim wheels As List = Array As String("Rims", "Tyres", "Tubes", "Air Valve", "Spokes", "Weights")
    Dim suspension As List = Array As String("Shocks", "Springs", "Pivots")
    Dim electrical As List = Array As String("Battery", "Lights", "Fuse box/Fuses", "Ignition")
    Dim custom As List = Array As String("Paint", "Racks", "Engine Bars", "seat")
    Dim maintenance As List = Array As String("Wheels & Tyres", "Oil", "Grease", "Bolts", "Transmission", "Cables", "Chain" )

    Dim allSections As List = Array As List(frame, engine, exhaust,fuel,brakes, wheels, suspension, electrical, custom, maintenance)


since I was getting the error, I changed the location and method in which I added the lists;

new code:
    allSections.Add(frame)
    allSections.Add(engine)
    allSections.Add(exhaust)
    allSections.Add(fuel)
    allSections.Add(brakes)
    allSections.Add(wheels)
    allSections.Add(suspension)
    allSections.Add(electrical)
    allSections.Add(custom)
    allSections.Add(maintenance)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is indeed a confusing issue. It will work if you create the list like this:
B4X:
Dim allSections As List = Array(frame, engine, exhaust,fuel,brakes, wheels, suspension, electrical, custom, maintenance) 'Array = Array As Object
I recommend you to always use Array when creating a static list.

The reason that it fails when you set the array type to List is that List is a "wrapper" object and it needs to be unwrapped when it is added to a generic collection. When you define the array type to List, the Lists aren't unwrapped. The solution is to always use the simpler "Array" without type.
 
Upvote 0

67biscuits

Member
So, don't declare the array as list, so simple now...thanks. I do have to understand the wrapping concept. Any reading to be done on that matter?
After changing the code I got a different error, out of bounds error. I changed the declaration to exclude the assignation of data to allsections, added the allsections.initialize to Sub Initialize and then assigned the array in BX4Page_Created. Now it works.
Is it bad form to be assigning values in Class_Globals??

Thanks Erel
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Is it bad form to be assigning values in Class_Globals??
It is fine.

I do have to understand the wrapping concept. Any reading to be done on that matter?
I don't think that there is much about this. The "wrapper" (ObjectWrapper) is usually a concept that only library developers encounter. There are some edge cases such as this one where the implementation detail surfaces.
 
Upvote 0
Top