B4J Question Problem multidimensional list

tanush62

Member
I Have a code like this:

B4X:
    curs1 = Main.SQL1.ExecQuery(query1)

    orart2V.Clear
    Do While curs1.NextRow
       
        orart2H.Clear
        For col = 0 To curs1.ColumnCount - 1
            orart2H.Add(curs1.GetString2(col))
            Private lbl As Label
            lbl.Initialize("Cell")
            CSSUtils.SetBorder(lbl, 1, fx.Colors.DarkGray, 5)
            lbl.Alignment = linel(col)
            innerPane1.AddNode(lbl, mw(col), row * h, mw(col+1) - mw(col) - 1, h - 1)
            lbl.Text = curs1.GetString(curs1.GetColumnName(col))
            lbl.TextSize = 16
        Next

        Log("list = " & orart2H)

        orart2V.Add(orart2H)

Log(orart2V.Get(row)) ' So far so good

        row = row + 1
        sumb = sumb + curs1.GetString2(4)
        nro = curs1.GetString2(0)
    Loop

    curs1.close
Log(orart2V.Size)
    Dim mn1, mg1 As Int
    For mn1 = 0 To orart2V.Size - 1
        Dim child As List = orart2V.Get(mn1)
        For mg1 = 0 To orart2H.Size - 1
            Log(mn1 & " - " & mg1 & " -- " & child.Get(mg1)) ' but here is no good
        Next
    Next

I have 5 rows in a query and six columns. I need them in a List too. In a last loop (for... next) I get only the 5th row. What I'm doing wrong? Please advice.
B4X:
' here is the log.

list = (ArrayList) [1, One, 1.000, 300.00, 300.00, 6]
[1, One, 1.000, 300.00, 300.00, 6]
list = (ArrayList) [2, Two, 2.000, 300.00, 600.00, 7]
[2, Two, 2.000, 300.00, 600.00, 7]
list = (ArrayList) [2, Three, 1.000, 100.00, 100.00, 3]
[2, Three, 1.000, 100.00, 100.00, 3]
list = (ArrayList) [2, Four, 1.000, 80.00, 80.00, 1]
[2, Four, 1.000, 80.00, 80.00, 1]
list = (ArrayList) [2, Five, 1.000, 100.00, 100.00, 2]
[2, Five, 1.000, 100.00, 100.00, 2]
5
0 - 0 -- 2
0 - 1 -- Five
0 - 2 -- 1.000
0 - 3 -- 100.00
0 - 4 -- 100.00
0 - 5 -- 2
1 - 0 -- 2
1 - 1 -- Five
1 - 2 -- 1.000
1 - 3 -- 100.00
1 - 4 -- 100.00
1 - 5 -- 2
2 - 0 -- 2
2 - 1 -- Five
2 - 2 -- 1.000
2 - 3 -- 100.00
2 - 4 -- 100.00
2 - 5 -- 2
3 - 0 -- 2
3 - 1 -- Five
3 - 2 -- 1.000
3 - 3 -- 100.00
3 - 4 -- 100.00
3 - 5 -- 2
4 - 0 -- 2
4 - 1 -- Five
4 - 2 -- 1.000
4 - 3 -- 100.00
4 - 4 -- 100.00
4 - 5 -- 2
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

emexes

Expert
Licensed User
Watching that video is a good idea.

The reason things are not working as you expect is that you are reusing the one (single) list for the horizontal collection, clearing it each row. Fixed by creating the list anew for each row, eg change:
B4X:
Do While curs1.NextRow
    orart2H.Clear
    For col = 0 To curs1.ColumnCount - 1
to:
B4X:
Do While curs1.NextRow
    Dim orart2H As List
    orart2H.Initialize

    For col = 0 To curs1.ColumnCount - 1
 
Upvote 0

tanush62

Member
Watching that video is a good idea.

The reason things are not working as you expect is that you are reusing the one (single) list for the horizontal collection, clearing it each row. Fixed by creating the list anew for each row, eg change:
Tnx a bunch Man. That's it. Working Now.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Dim orart2H As List
orart2H.Initialize
This has tripped me up in the past as well - resulting in only the last row.
When I see such now - I then remember to init lists and other such objects.
 
Upvote 0
Top