Android Question Combobox add items in B4A & B4J

Hi. I'm new to B4X. Been tinkering with B4A especially as Android programming is new to me and B4A/B4X offers a more familiar touch.
Anyway, I have been trying to figure out how to add items to a combobox and haven't been able to find anything that works on the forums. I found a thread from 2013 but i guess things have changed a lot since then. Specifically, I want to load a supplier list from a sqlite3 database into a combobox (b4j & b4a). What I want to do is something like this:
Private sql As String ' set up a string to place the query into
Private Result As ResultSet
'
sql = "SELECT SUPPLIERNAME FROM SUPPLIER ORDER BY SUPPLIERNAME;" ' the actual query

Result = SQL1.ExecQuery(sql) ' this is where things go bad for me. I cannot figure out how to load items/name lists into the combobox.
For Each Result ' No, this is not actual b4x code. Just putting my way of thinking on 'paper' . a Starting point...
cboSupplier.SetItems (Result.GetString)
Next

I do apologise if there is an actual reply on the forums somewhere. After 2 days I haven't found anything that works

Thanks and regards

David
 
Solution
Hi David,

Here goes; it depends on the 'XUI Views' library and works for both B4A and B4J:
B4X:
    Dim rs As ResultSet
    Dim cb As B4XComboBox ' Assuming cb was initialised either in code or in the Designer
    Dim lst As List

    ' ... rs = sql.exec...

    lst.Initialize
    Do While rs.NextRow
        lst.Add(rs.GetString("your_column_name_here"))
    Loop
    rs.Close

    cb.SetItems(lst)

Hope this helps!

walt61

Active Member
Licensed User
Longtime User
Hi David,

Here goes; it depends on the 'XUI Views' library and works for both B4A and B4J:
B4X:
    Dim rs As ResultSet
    Dim cb As B4XComboBox ' Assuming cb was initialised either in code or in the Designer
    Dim lst As List

    ' ... rs = sql.exec...

    lst.Initialize
    Do While rs.NextRow
        lst.Add(rs.GetString("your_column_name_here"))
    Loop
    rs.Close

    cb.SetItems(lst)

Hope this helps!
 
Upvote 0
Solution
Thank you SO much Walt61. It works. A bit different from the vb6 way of doing it. I find it interesting that b4x seems to build an in-memory list first and then adds the items once the list is complete.
 
Upvote 0

walt61

Active Member
Licensed User
Longtime User
You're welcome! A couple of side remarks:
- I assume you'll order the data in your SELECT clause; if not, you could sort the list with 'lst.Sort' or 'lst.SortCaseInsensitive'.
- cb is a 'B4X...' view; these views are cross-platform wrappers of the native views. The native views exist as well (and with those, you can indeed add the items straight to the views' item lists) but their methods/properties/events can differ - and for ComboBoxes they do. That means your code wouldn't be cross-platform then, whereas with 'B4X...' views it's in most cases one size fits all.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
b4x seems to build an in-memory list first and then adds the items once the list is complete.
Not necessarily true if you use the native spinner of the B4XComboBox to populate the B4XCombobox. Here is a full example: that will automatically load the B4XComboBox without creating an in memory list.:
B4X:
Dim strQuery As String = $"CREATE TABLE IF NOT EXISTS SUPPLIER (SUPPLIERNAME TEXT PRIMARY KEY)"$
    SQL1.ExecNonQuery(strQuery)
    
    Dim strQuery As String = $"INSERT INTO SUPPLIER VALUES('General Motors'), ('Peugeot'), ('BMW'), ('Honda'), ('Toyota')"$
    SQL1.ExecNonQuery(strQuery)
    
    Dim SQL As String = "SELECT SUPPLIERNAME FROM SUPPLIER ORDER BY SUPPLIERNAME"
    Dim rs As ResultSet = SQL1.ExecQuery(SQL)
    Do While rs.NextRow
        B4XComboBox2.cmbBox.Add(rs.GetString("SUPPLIERNAME"))
    Loop
    rs.Close
B4X:
Sub B4XComboBox2_SelectedIndexChanged (Index As Int)
    Log(B4XComboBox2.GetItem(Index))
End Sub
 
Upvote 0

PaulMeuris

Active Member
Licensed User
It seems that the (in memory) list approach is slightly faster than the cmbbox approach.
A test with 5000+ records shows a difference of a few milliseconds.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
In the real world it is not practical to populate a B4XCombobox with 5000 records and scroll down the list to find what you want. There are more efficient ways.
 
Upvote 0
Next question, still pertaining to this matter. The designer window in generation calls all items B4XView. Fine, but how does the program know its dealing with a combobox? Do I change its name to B4XComboBox in the declaration (in Sub Class Globals) or is there some other way of declaring an item a combobox?
 
Upvote 0
Top