B4J Question how to assign a record id to a choice box

IslandMedic

Member
Licensed User
Longtime User
I am creating a choice box, this box has a list of staff that are available for work. Each staff member has a staff_id in the database. I can't figure out how to assign the staff_id to the choice box. I can add the name to the choices but how do I associate the staff_id?

I want to associate a human readable value like "Joe Shmoe" to a record id like 123.

I can't use the index of the choice box items as they aren't the same. I have been using the name of the staff to do queries, but it defeats the whole point of a staff_id in a db.

Any thoughts or suggestions would be greatly appreciated.

Brad
 

IslandMedic

Member
Licensed User
Longtime User
Is the tag normally for the object not each item in the object, or does the tag have an index as well so each item in the choice box has a tag?
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Could you not add the name and the id to the choicebox ie Joe Shmoe(123)
then use simple regex to get the id to use
B4X:
    Dim s As String = "Joe Shmoe(123)" ' dummy data from choicebox selected item
    Dim data() As Object
    data = Regex.Split(":",s.Replace(")","").replace("(",":"))
    Dim i As Int = data(1) ' the user id
    Log(i)
Or a shorter version (but harder to read)
B4X:
Dim s as String = "Joe Shmoe(123)"
Dim I as Int =  Regex.Split(":",s.Replace(")","").replace("(",":"))(1)
log(I)  ' the user id
 
Last edited:
Upvote 0

eurojam

Well-Known Member
Licensed User
Longtime User
you have to link the index of the choicebox to your record id. This can be done with a corresponding map where you hold your record ids and your items.
B4X:
    Dim result As Map
    result.Initialize
    Do While cursor.NextRow
        result.Put(cursor.GetInt("id"),cur.GetString("name"))
        ChoiceBox.Items.Add(cursor.GetString("name"))
    Loop
later you can get the record id in the
B4X:
Sub ChoiceBox_SelectedIndexChanged(Index As Int, Value As Object)
    dim id as log
    id = result.GetKeyAt(index)
End Sub
 
Upvote 0
Top