Android Question Data List values being duplicated [SOLVED]

Tim Chapman

Active Member
Licensed User
Longtime User
In the Table module is this code:

Table Module:
Public Sub AddRow(Values() As String)
    If Values.Length <> NumberOfColumns Then
        Log("Wrong number of values.")
        Return
    End If
    Data.Add(Values)
    If Main.TableAddCounter = 0 Then 'When value to the left is 0,
        'the first item shows in the Data in the debugger.
        'CHANGE THE 0 to new value but below 431.
        'Then look at the Data value in the debugger.
        'All the values will be the same.  This is the problem.
        'The values that are going into Data are from Main line 410.
        Log(0)
    End If

The value in the Data list is duplicating itself with the last item added to the list.
The first item is added with no problem.
If two items are added, they both become the same as the second item.
If three items are added, the all three become the same as the third item.

In the TableExample this does not happen. (https://www.b4x.com/android/forum/t...supports-tables-of-any-size.19254/#post110901)
I have put the same code in the table module from there and it behaves nicely.

I tried changing the name Data to Data1 thinking that it might be a reserved word.
I also removed all the libraries and code that were needed to load the table from the Excel spreadsheet.
Both gave no improvement.
 

Attachments

  • TimTodo.zip
    189.7 KB · Views: 28
Last edited:
Solution
Your method 'AllTodos' in Main looks like this:
B4X:
Sub AllTodos
...
    Dim Values(7) As String
...
    For i = 0 To TodoList.Size-1
...
        Values(0) = TempTodo.ID
        Values(1) = TempTodo.Name
        Values(2) = TempTodo.Status
        Values(3) = TempTodo.Context
        Values(4) = TempTodo.Category
        Values(5) = TempTodo.Priority
        Values(6) = TempTodo.Note
        table1.AddRow(Values)
   Next
...
End Sub

Change it like this: declare Values inside the loop, otherwise all Table rows refer to the same object (Values):
B4X:
Sub AllTodos
...
    For i = 0 To TodoList.Size-1
        Dim Values(7) As String
...
        Values(0) = TempTodo.ID
        Values(1) = TempTodo.Name
        Values(2) = TempTodo.Status...

walt61

Active Member
Licensed User
Longtime User
Your method 'AllTodos' in Main looks like this:
B4X:
Sub AllTodos
...
    Dim Values(7) As String
...
    For i = 0 To TodoList.Size-1
...
        Values(0) = TempTodo.ID
        Values(1) = TempTodo.Name
        Values(2) = TempTodo.Status
        Values(3) = TempTodo.Context
        Values(4) = TempTodo.Category
        Values(5) = TempTodo.Priority
        Values(6) = TempTodo.Note
        table1.AddRow(Values)
   Next
...
End Sub

Change it like this: declare Values inside the loop, otherwise all Table rows refer to the same object (Values):
B4X:
Sub AllTodos
...
    For i = 0 To TodoList.Size-1
        Dim Values(7) As String
...
        Values(0) = TempTodo.ID
        Values(1) = TempTodo.Name
        Values(2) = TempTodo.Status
        Values(3) = TempTodo.Context
        Values(4) = TempTodo.Category
        Values(5) = TempTodo.Priority
        Values(6) = TempTodo.Note
        table1.AddRow(Values)
   Next
...
End Sub
 
Upvote 0
Solution
Top