Android Question Array coding issue

GaryK4

Member
Licensed User
Longtime User
I am trying to set data for a B4XTable. The following must have a coding issue.
The first time through the loop, xxx array has the correct data. the second iteration (np=2) the yyy array is correct. However, the xxx array is not the same as the yyy array.
I must be missing something basic. I added separate string arrays for debugging. Otherwise, I would have coded data.add(data_item) at the end of the loop.

B4X:
    Dim pos As Int = 0
    Dim data_item(21) As String
    Dim xxx() As String
    Dim yyy() As String
    Dim zzz(21) As String
    

    
    For np = 1 To Main.num_players
        pos = 0
        data_item(pos) = players_scores(np).name
        For nn = 1 To 9
            pos = pos + 1
            data_item(pos) = players_scores(np).score(nn)
        Next
        pos = pos + 1
        data_item(pos) = players_scores(np).front
        
        For nn = 10 To 18
            pos = pos + 1
            data_item(pos) = players_scores(np).score(nn)
        Next
        pos = pos + 1
        data_item(pos) = players_scores(np).back
    
        If np=1 Then
            xxx = data_item
            
        End If
        If np=2 Then
            yyy = data_item
        End If

    Next
    
    Dim data As List
    data.Initialize
    
    data.Add(xxx)
    data.Add(yyy)
 

Sagenut

Expert
Licensed User
Longtime User
The error is that
B4X:
xxx = data_item
and
B4X:
yyy = data_item
will not copy the content of data_item into xxx and yyy separately.
xxx and yyy will become 2 References to data_item.
So both xxx and yyy will not have a real content but will point to the content of data_item
I would use List instead of Arrays
B4X:
Dim data_item as List
Dim xxx as List
Dim yyy as List
data_item.Initialize
xxx.Initialize
yyy.Initialize
 
For np = 1 To Main.num_players
    data_item.Add(players_scores(np).name)
    For nn = 1 To 9
        data_item.Add(players_scores(np).score(nn))
    Next
    data_item.Add(players_scores(np).front)
 
    For nn = 10 To 18
        data_item.Add(players_scores(np).score(nn))
    Next
    data_item.Add(players_scores(np).back)
 
    If np=1 Then
       xxx.AddAll(data_item)   'This will copy the content of data_item into xxx
    End If

    If np=2 Then
       yyy.AddAll(data_item)   'This will copy the content of data_item into yyy
    End If
    data_item.Clear
Next

Dim Data as List
Data.Initialize
Data.AddAll(xxx)   'This will copy the content of xxx into Data
Data.AddAll(yyy)   'This will copy the content of yyy into Data, adding it to the xxx one
I STRONGLY suggest you to name every variable with a concrete and senseful name.
It will help you during coding.
I hope that xxx, yyy and zzz was just for the example to be fast. :)
I kept the different lists as you have done.
Maybe you need xxx and yyy to have the info of every single players separated.
If not I would do it directly with one list
B4X:
Dim data as List
data.Initialize
 
For np = 1 To Main.num_players
    data.Add(players_scores(np).name)
    For nn = 1 To 9
        data.Add(players_scores(np).score(nn))
    Next
    data.Add(players_scores(np).front)
 
    For nn = 10 To 18
        data.Add(players_scores(np).score(nn))
    Next
    data.Add(players_scores(np).back)
Next
 
Last edited:
Upvote 0

GaryK4

Member
Licensed User
Longtime User
The error is that
B4X:
xxx = data_item
and
B4X:
yyy = data_item
will not copy the content of data_item into xxx and yyy separately.
xxx and yyy will become 2 References to data_item.
So both xxx and yyy will not have a real content but will point to the content of data_item
I would use List instead of Arrays
B4X:
Dim data_item as List
Dim xxx as List
Dim yyy as List
data_item.Initialize
xxx.Initialize
yyy.Initialize
 
For np = 1 To Main.num_players
    data_item.Add(players_scores(np).name)
    For nn = 1 To 9
        data_item.Add(players_scores(np).score(nn))
    Next
    data_item.Add(players_scores(np).front)
 
    For nn = 10 To 18
        data_item.Add(players_scores(np).score(nn))
    Next
    data_item.Add(players_scores(np).back)
 
    If np=1 Then
       xxx.AddAll(data_item)   'This will copy the content of data_item into xxx
    End If

    If np=2 Then
       yyy.AddAll(data_item)   'This will copy the content of data_item into yyy
    End If
    data_item.Clear
Next

Dim Data as List
Data.Initialize
Data.AddAll(xxx)   'This will copy the content of xxx into Data
Data.AddAll(yyy)   'This will copy the content of yyy into Data, adding it to the xxx one
I STRONGLY suggest you to name every variable with a concrete and senseful name.
It will help you during coding.
I hope that xxx, yyy and zzz was just for the example to be fast. :)
I kept the different lists as you have done.
Maybe you need xxx and yyy to have the info of every single players separated.
If not I would do it directly with one list
B4X:
Dim data as List
data.Initialize
 
For np = 1 To Main.num_players
    data.Add(players_scores(np).name)
    For nn = 1 To 9
        data.Add(players_scores(np).score(nn))
    Next
    data.Add(players_scores(np).front)
 
    For nn = 10 To 18
        data.Add(players_scores(np).score(nn))
    Next
    data.Add(players_scores(np).back)
Next

B4X:
    Dim pos As Int = 0
    Dim data_item As List
    data_item.Initialize
    
    Dim player_data(6) As List
    For nn = 0 To 5
        player_data(nn).Initialize
    Next
    
    Dim data As List
    data.Initialize

    
    For np = 1 To Main.num_players
        pos = 0
        data_item.Add(players_scores(np).name)
        For nn = 1 To 9
            pos = pos + 1
            data_item.Add(players_scores(np).score(nn))
        Next
        pos = pos + 1
        data_item.Add(players_scores(np).front)
        
        For nn = 10 To 18
            pos = pos + 1
            data_item.Add(players_scores(np).score(nn))
        Next
        pos = pos + 1
        data_item.Add(players_scores(np).back)
        
        player_data(np).addall(data_item)
        data.Add(player_data(np))
        
        data_item.clear

    Next


Thanks, that worked!

I am new to B4xTables. Each row currently has (21) columns in it. Therefore I think it has to be like the new test code above to get the correct rows and columns.
I still have a lot of polishing to do, but this is a great start.
You also cleared up the add and addall for me.
The xxx, yyy quick test variable are removed :D

Thanks again - you made my day!
 
Upvote 0
Top