Android Question Strange behavior of Map

Q Candiani

Licensed User
Longtime User
Hi, I am working in a B4A app wich interaction with a PostgreSQL DB. It uses the jRDC2 library. For developing reasons the server is mounted on LocalHost. The middle server developed with B4J works fine.
The code is very extended and works fine in general. But in some random cases, when I try to iterate and load the SQL query result into a Map (where the key is the index of row and the value is an array with the others columns of the row), occurs that in each iteration the items of the map preserve correctly the keys but the values of all items change like the new one.
It's very strange. That happens in specific maps even though the way and structure to iterate and to load are the same that successful other cases in my code.

That is the code where I try to load the map:

Sub load_clients
        '#Tabla: b004: Search basic info of clients
        '#Input: us_id
        '#Return: indece, cuit_client, ratio_social, name, condition_iva, fantasy_name
        'sql.user_clients_b004=Select indice, cuit_client, ratio_social, name, condition_iva, fantasy_name FROM b004 /
        'WHERE us_id = CAST(? As BIGINT) And date_down Is NULL;
        'arg: It was define as private variant in current activity: private arg As Object
        arg = Array(Starter.us_indice)
        'exe_sql_query: It was define as sub in the current Activity: Return query_success as boolean and query_result as list; Both define as private variant in current activity
        wait for exe_sql_query_complete
        LogColor("user_clients_b004: " & query_success, Colors.Green)
        If query_success Then
            If query_result.Size > 0 Then
                Dim index_row As Int
                Dim text_cast As String
                Dim client_name As String
                Dim basic_info As List
                For Each fila() As Object In query_result
                    '0. indice
                    '1. cuit_client
                    '2. ratio_social
                    '3. name
                    '4. condition_iva
                    '5. fantasy_name
                    basic_info.AddAll(Array As String(fila(1),fila(2),fila(3),fila(4),fila(5)))
                    index_row = fila(0)
                    '-------------------THIS MAP HAS THE UNKNOWN PROBLEM
                    us_clients.Put(index_row, basic_info)
                    '-----------For the search machine
                    'Filling the map in two times: by cuit_client; by full name
                    text_cast = fila(1)
                    If fila(5) <> Null Then
                        client_name = " (" & fila(5) & ")"
                        client_name = ""
                    End If
                    ''-------------------THIS MAP WORKS PERFECTLY
                    us_machine_clients.Put(fila(2) & ", " & fila(3) & client_name, fila(0))
            End If
        End If
        Dim message_error As String = "load_clients " & " - [Error]: " & LastException
        LogColor(message_error, Colors.RGB(223,129,27))
    End Try
    CallSubDelayed(Me, "load_clients_complete")
End Sub


Licensed User
Longtime User
In your for next, you need to declare a new basic_info list, not clear an existing one. Complex objects are handled as references, so you are adding a reference of the basic_info list to your map, not a copy of the list. So all items in your map point to the same list.
Upvote 0