Android Question How to read mulitple type variables?

maleche

Active Member
Licensed User
Longtime User
How do I iterate through a multi Type variable?
When using just “Specs” (below) as one type I can iterate through the list and view the assigned values using

Dim SP as Specs
SP.Color=”Blue”
SP.VIN=”23546789ABCED”
SP.EngineSize=6.3
Log(PS.color & “ “ & SP.VIN & “ “ & SP.EngineSize) <=This works

Adding the “Cars” Type to the Specs Type fails (see example below)

Example:

Sub Globals
TypeCars(Make AsString, Model AsString, Year AsString)
TypeSpecs(Vehicle AsCars, Color AsString, VIN AsString, EngineSize AsDouble, Transmission AsBoolean)
End Sub
Sub Activity_Create(FirstTime AsBoolean)
'==========Load===============
Inventory.Vehicle.Make="FORD" '<========Fails here!
Inventory.Vehicle.Model="F-150 Ranger"
Inventory.Vehicle.Year="2013"
Inventory.Color="Blue"
Inventory.VIN="GNX45678014AJ145654"
Inventory.EngineSize="5.3"
Inventory.Transmission=True
'==========Test===============
For each v as Specs in Inventory '??? should I use this?
For each v as String in Inventory.Vehicle '??? or this?
Log(v.Make) '? how can I view the results here?
Next
‘================
End Sub

Inventory.Vehicle.Make="FORD"
java.lang.NullPointerException
at obd.obdcanex_sierra.main._activity_create(main.java:1119)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at obd.obdcanex_sierra.main.afterFirstLayout(main.java:100)
at obd.obdcanex_sierra.main.access$100(main.java:17)
at obd.obdcanex_sierra.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4967)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1011)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
at dalvik.system.NativeStart.main(Native Method)
 

Roycefer

Well-Known Member
Licensed User
Longtime User
You have to call
B4X:
Inventory.Initialize
Inventory.Vehicle.Initialize
before you start trying to access any of the fields.
 
Upvote 0

maleche

Active Member
Licensed User
Longtime User
Good thought!
But it did not work.

here is the error:

main_activity_create (B4A line: 414)
Inventory.Vehicle.Initialize
java.lang.NullPointerException
at obd.obdcanex_sierra.main._activity_create(main.java:1116)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at obd.obdcanex_sierra.main.afterFirstLayout(main.java:100)
at obd.obdcanex_sierra.main.access$100(main.java:17)
at obd.obdcanex_sierra.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4967)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1011)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Are you initializing in the order I specified? I was able to reproduce your first error and adding
B4X:
Inventory.Initialize
Inventory.Vehicle.Initialize
solved it. The only way I'm able to cause your second error is if I call Inventory.Vehicle.Initialize before Inventory.Initialize was called.

As to your other question about iterating through the fields of a Type using a For Each loop, I don't think that's possible. For Each expects an IterableList, which a Type is not. You could try creating a small class with public data and a single public method that returns a List of Strings with all the data. You could iterate through that List. You can also iterate through the List of keys or values in a Map.
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
You should do that :
B4X:
Sub Process_Globals

Type Cars(Make As String, Model As String, Year As String)
Type Specs(Vehicle As Cars, Color As String, VIN As String, EngineSize As Double, Transmission As Boolean)

Public ListInvent As List  'your vehicule list

End Sub

Sub Globals


End Sub




Sub Activity_Create(FirstTime As Boolean)
'==========Load===============
ListInvent.Initialize

Dim Inventory As Specs 'define new
Inventory.initialize  'initialze new

Inventory.Vehicle.Make="FORD"
Inventory.Vehicle.Model="F-150 Ranger"
Inventory.Vehicle.Year="2013"
Inventory.Color="Blue"
Inventory.VIN="GNX45678014AJ145654"
Inventory.EngineSize="5.3"
Inventory.Transmission=True
ListInvent.Add(Inventory)

'add new vehicule
Dim Inventory As Specs  'don't forget do dim again
Inventory.initialize  ' don't forget to initialze again

Inventory.Vehicle.Make="FERRARI"
Inventory.Vehicle.Model="350 GTO"
Inventory.Vehicle.Year="1986"
Inventory.Color="Red"
Inventory.VIN="XXXXXX"
Inventory.EngineSize="6.9"
Inventory.Transmission=True
ListInvent.Add(Inventory)
'==========Test===============

For Each v As Specs In ListInvent
    Log(v.Vehicle.Make)
    Log(v.color)
Next

'=========================
End Sub
 
Upvote 0

maleche

Active Member
Licensed User
Longtime User
THANK YOU!!
I can't believe I miss the other initialize!
works great!
Thank you for the great example!
Best,
Doyle
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
You are welcome, do not forget ListInvent will be pass by reference, if you have to modify an instance, create a "clone" list.

Cheers
Patrick
 
Upvote 0
Top