Android Question B4XComboBox IndexOf problem

james_sgp

Active Member
Licensed User
Longtime User
Hi,
I`m using a B4xComboBox for a list of years of birth (1940 to 2023), but the I try (user_details.age contains an int '1970')

B4X:
user_age.SelectedIndex = user_age.IndexOf(Starter.User_Details.Age)

I`m getting back a -1, not found. I have also tried it with a string instead of integer and get the same issue.

Any advice would be appreciated.


James
 

DonManfred

Expert
Licensed User
Longtime User
Upload a small project showing the problem.
Hard to follow/advice without seeing your code
What is user_age? A List? What exactly is stored in there?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
How do you fill the B4XComboBox ?
I found two workarounds:
Add the years as strings in the list.
B4X:
    Private lst As List
    lst.Initialize
    Private num As String
    For i = 0 To 50
        num = i + 1980
'        lst.Add(i + 1980)  'this does not work
        lst.Add(num)  'this works
    Next
    B4XComboBox1.SetItems(lst)
    
    Private num1 As Int
    num1 = 2000
    Log(B4XComboBox1.IndexOf(num1))

Or add the years as Int directly to the inner Spinner of the B4XComboBox, valid only in B4A.
But here you loose the cross-platform feature.

B4X:
    Private lst As List
    lst.Initialize
    For i = 0 To 50
        B4XComboBox1.cmbBox.Add(i + 1980)
    Next
    
    Private num1 As Int
    num1 = 2000
    Log(B4XComboBox1.IndexOf(num1))
 
Upvote 0

emexes

Expert
Licensed User
I`m getting back a -1, not found. I have also tried it with a string instead of integer and get the same issue.
Any advice would be appreciated.

This works here; with luck it might work for you too:

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private EditText1 As EditText          'add using layout designer
    Private Label1 As Label                'add using layout designer
    Private B4XComboBox1 As B4XComboBox    'add using layout designer
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
 
    Dim L As List
    L.Initialize
 
    Dim ThisYear As Int = DateTime.GetYear(DateTime.Now)
    For Y = ThisYear - 110 To ThisYear
         L.Add("" & Y)    'combobox expects list of string type, not numeric type
    Next

    B4XComboBox1.SetItems(L)
    B4XComboBox1.SelectedIndex = B4XComboBox1.IndexOf("" & (ThisYear - 30))    'starting position = age 30
 
End Sub

Private Sub EditText1_TextChanged (Old As String, New As String)
 
    Dim I As Long = B4XComboBox1.IndexOf(New.Trim.ToLowerCase)
    Label1.Text = I
 
End Sub
 
Upvote 0

james_sgp

Active Member
Licensed User
Longtime User
Thanks everyone, but seems no joy... Attached is a simple B4X app showing the issue.

ComboBox is filled using my method (for ref), click the "Lookup (1970)" button to get the 'indexof' or not...

Thanks...
 

Attachments

  • test2.zip
    10.2 KB · Views: 65
Upvote 0

emexes

Expert
Licensed User
Try changing your line:
B4X:
list1.Add(i)
per my previous suggestion to:
B4X:
list1.Add("" & i)    'combobox expects list of string type, not numeric type

and once you've confirmed your problem is fixed, <JOKE> let me know where to send my support incident invoice. </JOKE>
 
Last edited:
Upvote 0
Top