Android Question List, Array and Type Question

Binary01

Active Member
Licensed User
Longtime User
Hi,

I test a small program with type and list data type.

At Globals
'declare type
Type Person (First As String, Second As String, age As Int )

At Activity_Create
Dim List1 As List
List1.Initialize
List1.AddAll ( Array As Person() )
Public tempdata,tdata As Person

tempdata.First ="Smith"
tempdata.Second ="John"
tempdata.age =22
List1.Add (tempdata)

tempdata.First ="Marry"
tempdata.Second ="Lin"
tempdata.age =18
List1.Add (tempdata) 'Adds a second value at the end of the list

Dim s As Int
s = list1.Size
Log("List Size")
Log(s)
Log("1st Person")
tdata= List1.Get(0)
Log(tdata.First )
Log(tdata.Second )
Log(tdata.age )

Log("2nd Person")
tdata= List1.Get(1)
Log(tdata.First )
Log(tdata.Second )
Log(tdata.age )

But result is same and last data only as follow.
List Size
2
1st Person
Marry
Lin
18
2nd Person
Marry
Lin
18

How can I fix it?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
you are dimming tempdata once but uses more than one reference to it... List.Add adds a reference to tempdata to the list. Not the value.

Solution
B4X:
dim tempdata As Person
tempdata.First ="Smith"
tempdata.Second ="John"
tempdata.age =22
List1.Add (tempdata)

dim tempdata As Person
tempdata.First ="Marry"
tempdata.Second ="Lin"
tempdata.age =18
List1.Add (tempdata) 'Adds a second value at the end of the list

dim tdata as person =List1.Get(0)
' ...
dim tdata as person =List1.Get(1)
 
Last edited:
Upvote 0

Binary01

Active Member
Licensed User
Longtime User
Thanks DonManfred,
It well done.

I fix it as your suggest.

I found that a List can hold any object. So I remove a line:
List1.AddAll ( Array As Person() )
 
Upvote 0
Top