B4J Question [BANano] [SOLVED] Confused about the Final Map Order

Mashiane

Expert
Licensed User
Longtime User
Hi

I am a little baffled, please advise

This is my map, using numbers and text

B4X:
Sub InitMonths
    Months.Initialize
    Months.Put("01", "January")
    Months.Put("02", "February")
    Months.Put("03", "March")
    Months.Put("04", "April")
    Months.Put("05", "May")
    Months.Put("06", "June")
    Months.Put("07", "July")
    Months.Put("08", "August")
    Months.Put("09", "September")
    Months.Put("10", "October")
    Months.Put("11", "November")
    Months.Put("12", "December")
End Sub

I then call this method in my "select" component

B4X:
Sub SetMonths
    Clear
    Dim Months As Map = SDUIShared.Months
    Dim mTot As Int = Months.Size - 1
    Dim mCnt As Int = 0
    For mCnt = 0 To mTot
        Dim mkey As String = Months.GetKeyAt(mCnt)
        Dim mval As String = Months.GetValueAt(mCnt)
        Log(mkey)
        Log(mval)
        AddItem(mkey, mval)
    Next
End Sub

The end result is incorrect. I was using "for each" clause and decided to check with indexes instead.

1688576653563.png

This is the console log output...

1688576963739.png

How do I make this to start at "01" and not "10"


:oops:
 
Solution
It seems normal to me, this is the same behavior as B4J you can't expect the map to be sorted, so you should use B4XOrderedMap but it is not supported by BANano.

Mashiane

Expert
Licensed User
Longtime User
For now, I will hard code this as....

B4X:
Sub SetMonths
    Clear
    AddItem("01", "January")
    AddItem("02", "February")
    AddItem("03", "March")
    AddItem("04", "April")
    AddItem("05", "May")
    AddItem("06", "June")
    AddItem("07", "July")
    AddItem("08", "August")
    AddItem("09", "September")
    AddItem("10", "October")
    AddItem("11", "November")
    AddItem("12", "December")
End Sub

Though I didnt want to do this
 
Upvote 0

angel_

Well-Known Member
Licensed User
Longtime User
It seems normal to me, this is the same behavior as B4J you can't expect the map to be sorted, so you should use B4XOrderedMap but it is not supported by BANano.

 
Upvote 1
Solution

Mashiane

Expert
Licensed User
Longtime User
Thank you so much...

I used the B4xOrderedMap code as is, works perfectly

B4X:
Sub SetMonths
    Clear
    Dim Months As SDUIMap = SDUIShared.Months
    Dim mItems As List = Months.Keys
    For Each k As String In mItems
        Dim v As String = Months.Get(k)
        AddItem(k, v)
    Next
End Sub

Had to also add these methods..

B4X:
public Sub GetKeyAt(idx As Int) As Object
    Dim s As Int = getSize - 1
    If idx >= 0 And idx <= s Then
        Dim itemKey As Object = xlist.Get(idx)    
        Return itemKey
    Else
        Return Null
    End If
End Sub

public Sub GetValueAt(idx As Int) As Object
    Dim s As Int = getSize - 1
    If idx >= 0 And idx <= s Then
        Dim itemKey As Object = xlist.Get(idx)
        Dim item As Object = xmap.Get(itemKey)
        Return item
    Else
        Return Null
    End If
End Sub
 
Upvote 0
Top