Android Question Dynamic array with List

sn_nn

Member
Licensed User
Longtime User
Hello!
i'm trying create dynamic array with typed data. But List contain only last value. Help me, please! )))
B4X:
Sub Process_Globals
    Type tpType(Name As String, a As Int)
    Dim List As List
End Sub

Sub Globals
    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main")
End Sub

Sub Button1_Click
 'Creating the Dynamic Array (filling List)
 Dim tp As tpType
    List.Initialize
    List.Clear
    tp.Name="one"
    tp.a=1
    List.Add(tp)
    tp.Name="two"
    tp.a=2
    List.Add(tp)
    tp.Name="free"
    tp.a=3
    List.Add(tp)
    tp.Name="for"
    tp.a=4
    List.Add(tp)
    tp.Name="five"
    tp.a=5
    List.Add(tp)

'Output List to Log's
Dim a As Int
For a=0 To List.Size-1
  Dim tp1 As tpType
    tp1=List.Get(a)
    Log(a & ": " & tp1.Name &", " & tp1.a)
Next'a
End Sub

Logs are such:

0: five, 5
1: five, 5
2: five, 5
3: five, 5
4: five, 5
 

klaus

Expert
Licensed User
Longtime User
You need to Dim every entry to create a new instance !

B4X:
Sub Button1_Click
'Creating the Dynamic Array (filling List)
    List.Initialize
    List.Clear
    Dim tp As tpType
    tp.Name="one"
    tp.a=1
    List.Add(tp)
    Dim tp As tpType
    tp.Name="two"
    tp.a=2
    List.Add(tp)
    Dim tp As tpType
    tp.Name="three"
    tp.a=3
    List.Add(tp)
   'etc
 
Upvote 0
Top