Android Question [Solved] Missing ItemClick event in B4XComboBox

jruiz1998

Member
Licensed User
Hi, With B4XComboBox, Does ItemClick event exist? and how about loading value and name for getting SelectedId

Regards
JRuiz
 

walterf25

Expert
Licensed User
Hi, With B4XComboBox, Does ItemClick event exist? and how about loading value and name for getting SelectedId

Regards
JRuiz
You need to look at the class, you will see there is a function that looks something like this
B4X:
Private Sub RaiseEvent
    CallSub2(mCallBack, mEventName & "_SelectedIndexChanged", getSelectedIndex)
End Sub
In your Activity you will need to manually add a function with the same signature, maybe something like this:
B4X:
'Change cmbox to whatever name you gave when initializing the B4XComboBox variable
cmbox_SelectedIndexChanged(Index as Int)

End Sub
Regards,
Walter
 

jruiz1998

Member
Licensed User
Thanks Walter, it worked fine.
I had tested but using (Index As Int, Value As Object) instead of (Index as Int) only.

So, my second question is about loading "Value and Name" instead of "Name" only. (Not Index).
Meanwhile, I will "fight" with MAP to get Value and Name through Index.

Regards
JRuiz
 

walterf25

Expert
Licensed User
Thanks Walter, it worked fine.
I had tested but using (Index As Int, Value As Object) instead of (Index as Int) only.

So, my second question is about loading "Value and Name" instead of "Name" only. (Not Index).
Meanwhile, I will "fight" with MAP to get Value and Name through Index.

Regards
JRuiz
B4X:
Public Sub GetItem(Index As Int) As String
    #if B4J
    Return cmbBox.Items.Get(Index)
    #Else If B4A
    Return cmbBox.GetItem(Index)
    #Else
    Return mItems.Get(Index)
    #End If
End Sub
The above function will return the selected item "Value"

Walter
 

jruiz1998

Member
Licensed User
That's correct, I called that "Name" (description).
What I need is an "Id" of the description, like "item sku" of the description, different of Index.

JRuiz
 

jruiz1998

Member
Licensed User
Only to close this report.
I followed the Erel video to get Index, ID and valueName.

Sub Globals
Private B4XComboBox3 As B4XComboBox
Dim Label3 As Label
Private MapValues As Map
Private ListNames As List

' Data can come from a table, database or from anywhere
' Data to load (left ":" is ID, right, is Value or Name to be displayed)
Dim CBPreg2(7) As String
CBPreg2(0) = "15:2do Apellido abuela materna"
CBPreg2(1) = "23:Nombre primer mascota"
CBPreg2(2) = "37:pasatiempo favorito"
CBPreg2(3) = "99:Juguete preferido de tu niñez"
CBPreg2(4) = "113:Sobrenombre que tenías"
CBPreg2(5) = "980:Nombre de tu mejor amigo(a)"
CBPreg2(6) = "1320:Ciudad Europea que quieres visitar"
End Sub

Sub Activity_Create(FirstTime As Boolean)
LoadData
End Sub

Sub LoadData
MapValues.Initialize
ListNames.Initialize
Dim k As Int
For J = 0 To CBPreg2.Length - 1
k = CBPreg2(J).IndexOf(":")
Dim name As String = CBPreg2(J).SubString(k + 1) 'RIGHT
MapValues.Put(name, CBPreg2(J).Substring2(0, k)) 'LEFT ' fill MapValues (Map type)
ListNames.Add(name) ' fill ListNames (List type)
Next

' Populate Combo
B4XComboBox3.SetItems(ListNames)
End Sub

Sub B4XComboBox3_SelectedIndexChanged (Index As Int) ' It comes here after item click
Dim name As String = B4XComboBox3.GetItem(Index)
Dim idValue As Double = MapValues.Get(name)
Label3.Text = name & " " & idValue ' name and idValue are displayed in this Label
End Sub
 

Mahares

Well Known Member
Licensed User
Only to close this report.
I followed the Erel video to get Index, ID and valueName.
You can use something like this as entire code without having to deal with maps and substrings. If it does not meet your need, please ignore:
B4X:
Sub Globals
    Private MyList As List
    Private B4XComboBox3 As B4XComboBox  'need xui views lib
    Private Label3 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")  'has 1 combobox and 1 label 
    Dim CBPreg2(7) As String
    CBPreg2(0) = "15:2do Apellido abuela materna"
    CBPreg2(1) = "23:Nombre primer mascota"
    CBPreg2(2) = "37:pasatiempo favorito"
    CBPreg2(3) = "99:Juguete preferido de tu niñez"
    CBPreg2(4) = "113:Sobrenombre que tenías"
    CBPreg2(5) = "980:Nombre de tu mejor amigo(a)"
    CBPreg2(6) = "1320:Ciudad Europea que quieres visitar"
   
   
    MyList.Initialize
    For i=0 To  CBPreg2.Length-1
        Dim s() As String =Regex.Split(":",CBPreg2(i))
        MyList.Add(s(1) & " " & s(0))
    Next
    ' Populate Combo
    B4XComboBox3.SetItems(MyList)
End Sub

Sub B4XComboBox3_SelectedIndexChanged (Index As Int) 
    Label3.Text = B4XComboBox3.GetItem(Index) ' name & idValue displayed in this Label the way you want them
End Sub
 
Top