iOS Question help with my smelly code cycling through a map

tsteward

Well-Known Member
Licensed User
Longtime User
Attached code works fine on B4A & B4J but is not compatible with B4I as map.GetKeyAT & map.GetValueAt does not exist.
This code is used to fill a CLV
I'm not sure how else to cycle though my database returning in this case the Manufacturer name and ManufacturerID

I start with determining number of rows with 4 image views and labels per row and creating blanks at the end if I cannot fill the row.

B4X:
Do While rs.NextRow
        makeList.Put(rs.GetString("MakeName"),rs.GetLong("MakeID"))
    Loop
    rs.Close
    CachedPanels.Initialize
    Dim rows As Int = makeList.Size/4 ' number of makes / 4
    Dim remainder As Int = makeList.Size Mod 4 ' if does not divide by four then add 1 more row
    If remainder > 0 Then
        rows = rows + 1
    End If
    Dim counter As Int = makeList.Size
    Dim makeCounter As Int = 0
    For i = 1 To rows
        If counter > 3 Then
            makeList.
            Dim id1 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter),makeList.GetValueAt(makeCounter))
            Dim id2 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter+1),makeList.GetValueAt(makeCounter+1))
            Dim id3 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter+2),makeList.GetValueAt(makeCounter+2))
            Dim id4 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter+3),makeList.GetValueAt(makeCounter+3))
            counter = counter - 4
            makeCounter = makeCounter + 4
        Else
            If counter = 3 Then
                Dim id1 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter),makeList.GetValueAt(makeCounter))
                Dim id2 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter+1),makeList.GetValueAt(makeCounter+1))
                Dim id3 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter+2),makeList.GetValueAt(makeCounter+2))
                Dim id4 As ImageData = CreateItem2("",0)
                counter = counter - 3
                makeCounter = makeCounter + 3
                'id4.Title = ""
                'id4.Bitmap = Null
            End If
            If counter = 2 Then
                Dim id1 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter),makeList.GetValueAt(makeCounter))
                Dim id2 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter+1),makeList.GetValueAt(makeCounter+1))
                Dim id3 As ImageData = CreateItem2("",0)
                Dim id4 As ImageData = CreateItem2("",0)
                counter = counter - 2
                makeCounter = makeCounter + 2
                'id4.Title = ""
                'id3.Title = ""
                'id4.Bitmap = Null
                'id3.Bitmap = Null
            End If
            If counter = 1 Then
                Dim id1 As ImageData = CreateItem2(makeList.GetKeyAt(makeCounter),makeList.GetValueAt(makeCounter))
                Dim id2 As ImageData = CreateItem2("",0)
                Dim id3 As ImageData = CreateItem2("",0)
                Dim id4 As ImageData = CreateItem2("",0)
                counter = counter - 1
                makeCounter = makeCounter + 1
                'id4.Title = ""
                'id4.FileName = ""
                'id3.FileName = ""
                'id2.FileName = ""
                'id3.Title = ""
                'id2.Title = ""
            End If
        End If
        Dim p As B4XView = xui.CreatePanel("")
        p.SetLayoutAnimated(0, 0, 0, clvMakes.AsView.Width, 110dip)
        clvMakes.Add(p, Array As ImageData(id1, id2, id3, id4))
    Next
 

LucaMs

Expert
Licensed User
Longtime User
Cycle on the Map and increase counters inside it:
B4X:
For Each Key As String In makeList.Keys '<--- bad name for a Map!
    ' here increase rows - counter as you need
    ' use makeList.Get(Key) to create the item.
    ' ...
Next
 
Last edited:
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
B4X:
makemap = CreateMap("0":"A-0","1":"A-1","2":"A-2","3":"A-3","4":"A-4","5":"A-5")
Log(makemap)
  
    For Each key As String In makemap.Keys
        Dim value As String = makemap.Get(key)
        Log($"Key: ${key} Value:${value}"$)
    Next
 
Upvote 0

tsteward

Well-Known Member
Licensed User
Longtime User
Sorry guys maybe I'm a bit thick but I can't figure out how to get four keys at a time to make data for one row in my clv so I can then add that row.
If it were one item per row, then that would be easy.

And of course, then deal with the fact that at some point the last row might not have four members.
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
Starting from the assumption that i would modify your code (it seems quite complicated), in any case if you were to use the same, you could do a similar thing:
B4X:
Dim count4 As Int = 0
    For Each Key As String In makemap.Keys 
        Dim value As String = makemap.Get(Key)
        Select count4
            Case 0
                Log($"Key: ${Key} Value:${value}"$)
            Case 1
                Log($"Key: ${Key} Value:${value}"$)
            Case 2
                Log($"Key: ${Key} Value:${value}"$)
            Case 3
                Log($"Key: ${Key} Value:${value}"$)
        End Select
        count4 = count4 + 1
    Next
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
I think for B4i it would be better if @tsteward would switch to B4XCollections B4X ordered map which preserves the order of the keys and values inserted also in B4i. Then the code would be somewhat the same as yours @MarcoRome.

 
Upvote 0
Top