B4J Question ComboBox Problem

Toley

Active Member
Licensed User
Longtime User
I have a program that use a Map to load some values. At some point, I want to load some of the map values into a combobox.

For simplicity I have grouped my data in my map in group of 10 data for each group. Some are not used and reserved for future feature. So to retieve my data I simply divide the total size of the map by ten and add the index of the desired value. From a For loop I was supposed to be able to retrieve data of each group at a given index. But it seems not to work.

I have made a minimal code (this is not the original code but it show the issue).

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 400
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private ComboBox1 As ComboBox

    Private MyMap, TempMap As Map
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
    MyMap.Initialize
   
    MyMap = FillMap
   
    Private x, maxx, index As Int
    Private str_item As String
    ComboBox1.Items.Clear
    maxx = MyMap.Size / 10
    For x = 0 To maxx
        index = (x * 10) + 3
        str_item = MyMap.GetValueAt(index)
        ComboBox1.Items.Add(str_item)
    Next
End Sub


Sub FillMap As Map
    TempMap.Initialize
    TempMap.Clear
   
    'Group #0
    TempMap.Put(0,"Group0 Item0")
    TempMap.Put(1,"")
    TempMap.Put(2,"Group0 Item2")
    TempMap.Put(3,"Group0 Item3")
    TempMap.Put(4,"Group0 Item4")
    TempMap.Put(5,"Group0 Item5")
    TempMap.Put(6,"Group0 Item6")
    'Group #1
    TempMap.Put(10,"Group1 Item0")
    TempMap.Put(11,"")
    TempMap.Put(12,"Group1 Item2")
    TempMap.Put(13,"Group1 Item3")
    TempMap.Put(14,"Group1 Item4")
    TempMap.Put(15,"Group1 Item5")
    TempMap.Put(16,"Group1 Item6")
    'Group #x etc...
   
   
    Return TempMap
End Sub
Running this code in debug mode with a breakpoint at the end the For loop demonstrate the problem.
The ComboBox was created with the internal designer.

The first time the index is 3 and the return value is "Group0 Item3" as expected.
But the second time, while the index is 13 (good) it return "Group1 Item6" which is at index 16.

Thank you for any help.
 

Toley

Active Member
Licensed User
Longtime User
Hi Erel thank you for the fast answer. In my example, there is only 2 groups of data, but in the real app there will be many tenth of groups that will grow up with time. This will make a very large array that will be hard to maintain and calculate the positions. A map would have been easier for me because of the index.

If it's the only way to go I will try with an array but I really don't believe it's a user friendly solution.

Thank you.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Where you read the data, you are assuming that "Group1 Item6" is at position 16, when it is actually at position 13 (same as index).
You are reading by position not keyvalue, therefore you must include the missing items ie 7,8 and 9 in the map even if blank. Then
"Group 1 Item3" will be in position 13.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can of course use a map. But you need to structure it correctly.

You can for example create a map where the keys are the group ids and the values are lists or arrays with the items.
If you find yourself using GetValueAt or GetKeyAt then rethink about the design.

B4X:
Dim GroupValues As List = Map.Get("Group1")
 
Upvote 0
Top