iOS Question B4XTable and List

Graeme Mitchell

Member
Licensed User
I have 10 records but all this does is add the last record to all 10 lines of the table

List of Table:
Dim row(5) As Object
    
    Dim datalist As List
    datalist.Initialize
    
    For Each item As Map In data
    
    row(0) =item.Get("Name")
    row(1) =item.Get("Dept")
    row(2) =item.Get("Access")
    row(3) =item.Get("Clock")
    row(4) =item.Get("Password")
    datalist.Add(row)
    
    Next
        
    B4XTable1.SetData(datalist)
 

Daestrum

Expert
Licensed User
Longtime User
Move
B4X:
Dim row(5) As Object

to after the For Each Line
B4X:
For Each item As Map In data
Dim row(5) As Object

Explanation:
Imagine that row() lives at memory address 4
you move the data with the ,get to each array member
you then .add rec to the list - but .add uses a reference so points to (memory) 4
you then in the loop .get new data for the array
you then .add to list
now because the first item in the list was told its data was at (memory) 4 - you have effectively overwritten it with the subsequent value.

that's why when you look at the list, all items have the same value as the last item you added.

By putting the dim inside the loop, you force rec() to get a new reference each time, so the data for each list item will be written with unique reference, and the data will correctly show in the the list.
 
Last edited:
Upvote 1
Top