B4J Library [B4X] lmB4XComboBox

lmB4XComboBox is a b4x library (https://www.b4x.com/android/forum/threads/100383/#content).
It works with B4A, B4J and B4i.

It is a modified version of the Erel's original B4XComboBox and allows you to store in it a value for each text item.
Not rarely (mainly handling DB data) you need a ComboBox in which an Item is made of a display value and an associated value, i.e. a "description field" of a table and the relative primary key (usually the classic Integer ID).
Note that the type of the values associated is object, not just Int; this means that you can associate any type of value to each item.

Members added to the original View:

AddItem(Text As String)
- to add a single text item
AddItem2(Text As String, Value As Object) - to add a single item with text and value
GetItems - returns the full list of texts
GetItemText(Index As Int) - returns the text of an item
GetItemValue(Index As Int) - returns the value associated to an item
GetValues - returns all the values associated
RemoveItemByIndex(Index as Int) - removes an item
RemoveItemByText(Text As String) - removes an item
RemoveItemByValue(Value As Object) - removes an item
SetItems2(Texts As List, Values as List) - to set all items with values
SetItemsFromSQLite(...) - to fill the combo from a SQLite table
SetValue(Index as Int, Value as Object) - to set the value of a specific item


B4J Example (project attached):
1.gif


To run the example project, you need to download the Chinook_Sqlite.sqlite database from:
https://www.sqlitetutorial.net/sqlite-sample-database/
(and place it in the files folder, of course).

Version: 2.01 06/15/2021
Added: Method SetItems3(Data As Map)

Version: 2.00 03/05/2021
Fixed: a bug in InsertAt (B4A version only).

Version: 1.06 02/11/2021
Changed: SetItemsFromSQLiteSorted method - Added CaseSensitive parameter.

Version: 1.05 02/06/2021
Added: SetItemsFromSQLiteSorted method.

[I was wrong to remove the previous versions of the library, now I don't know how many times it has been downloaded 🤔😊
However I see that so far the example has been downloaded 185 times, so I can guess that]


Version: 1.04 02/03/2021
Added: InsertItemAt and InsertItemAt2 methods.

Version: 1.03 02/03/2021
Fixed a (small) bug.

Version: 1.02 4/25/2020
Ammended B4i errors (reported by @Andrew (Digitwell) ; thanks)
 

Attachments

  • lmB4XComboBox_example.zip
    3.6 KB · Views: 404
  • lmB4XComboBox.b4xlib
    3.4 KB · Views: 98
Last edited:

Daniel44

Active Member
Licensed User
Now it would remain to make an example in B4A. It would be very useful too. Any way thank you Lucas
 

Myr0n

Active Member
Licensed User
Greate job.

Here is the b4a example based from b4j example.

In b4a example I needed to change
B4X:
lmcbArtists.SetItemsFromSQLite(modDB.DB, "Artist", "Name", 15, "ArtistID", "Int")
to
B4X:
lmcbArtists.SetItemsFromSQLite(modDB.DB, "Artist", "Name", 15, "ArtistId", "Int")

because in the db "chinook_sqlite.sqlite" is ArtistId and not ArtistID
in b4a is case sensitive.


Also I changed the modDB.bas file to fit with b4a and b4j, I did not test in b4i because my license stopped in version 5.x
B4X:
Public Sub Init
    #if b4j
        DBDir = File.DirApp
    #End If
    #if b4a
        DBDir = File.DirDefaultExternal
    #End If
    DBFileName = "Chinook_Sqlite.sqlite"
    If Not(File.Exists(DBDir, DBFileName)) Then
        File.Copy(File.DirAssets, DBFileName, DBDir, DBFileName)
    End If
    #if b4j
        DB.InitializeSQLite(DBDir, DBFileName, False)
    #End If
    #if b4a
        DB.Initialize(DBDir, DBFileName, False)
    #End If
End Sub


Again good job.

BTW, In the zip file I am not including "Chinook_Sqlite.sqlite" see post #1
 

Attachments

  • B4A-lmB4XComboBox_example.zip
    10.5 KB · Views: 116
  • B4J-lmB4XComboBox_example.zip
    3.5 KB · Views: 120

LucaMs

Expert
Licensed User
because in the db "chinook_sqlite.sqlite" is ArtistId and not ArtistID
in b4a is case sensitive.
This was because I'm used to name the identifier (Primary Key) ID :)

Also I changed the modDB.bas file to fit with b4a and b4j,
That is just a "temporary utility module", not a B4X module to be reused (it was not the centerpiece of the thread).
 

Myr0n

Active Member
Licensed User
This was because I'm used to name the identifier (Primary Key) ID :)


That is just a "temporary utility module", not a B4X module to be reused (it was not the centerpiece of the thread).

You know, I copy & paste everything from your example to b4a example, the rest is apply the logic.
 

LucaMs

Expert
Licensed User
Really very useful library; who knows why I haven't used it in the project I'm developing. Maybe it's because my memory is really poor 😄 :confused: :confused: :confused:

Maybe the author could add functionality and/or improve it, who knows.



[I wonder why the example file has been downloaded more times than the library one; what do they do with the example without the library? Well, mystery]
 

LucaMs

Expert
Licensed User
Maybe the author could add functionality and/or improve it, who knows.
You could add an "InsertAt" method, for example.
Take advantage of a function you have, which binds SQLite data types to B4X data types.
And pass it an open ResultSet, from which to take the "two values" to fill it (a little risky but useful).
 
Last edited:
Top