Android Question List of lists problem

zetadan

Member
Licensed User
Longtime User
I have a problem that is very strange to me as I have used this method successfully before but now it does not behave as I expect. I am creating a list of lists but the masterlist seems to follow the state of the sublist no matter what i do. See the following test code
B4X:
Sub Globals
    Dim MainList As List
    Dim l1 As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    l1.Initialize
    MainList.Initialize
    l1.Add("a")
    MainList.Add(l1)
    l1.Clear
    l1.Add("b")
    MainList.Add(l1)
End Sub
The state of the lists is the following after each line in activity_create assuming the following format (l1,mainlist)
line 1 ([],[])
line 2 ([],[])
line 3 (["a"],[])
line 4 (["a"],["a"])
line 5 ([],[])
line 6 (["b"],["b"])
line 7 (["b"],["b","b"])

This is what I expect:
line 1 ([],[])
line 2 ([],[])
line 3 (["a"],[])
line 4 (["a"],["a"])
line 5 ([],["a"])
line 6 (["b"],["a"])
line 7 (["b"],["a","b"])

Please help this is driving me crazy.

Dan
 

zetadan

Member
Licensed User
Longtime User
Thanks so much guys,

This makes sense, however, the actual code I am using runs in a loop and I will have to perform the list operation multiple times of indefinite number. Can I re-dimension and re-initialize inside the loop without problem? Or should I just re-initialize inside the loop?

To be clear I want to end up with an array (list) of many lists. Not just single items as in the example.

Just asking, I can't try either of these ideas until later today.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Even better, just move the dim and unit into the loop. No need for it outside.
 
Upvote 0
Top