B4J Question Using Combo boxes to fill fields

Peter Lewis

Active Member
Licensed User
Longtime User
Hi

I asked a similar question before but now I wanted to know which are best practices within this enviroment.

The solution I want is to have a form with fields and some / most of the 40 fields should have preloaded options. for example Colour where there must only be 4 options, Red Black Blue White. Instead of asking the user to enter in the colour (and they spell it wrong or enter in the wrong colour ,I wanted them to only have those choices with a pulldown. Their the choices can come from a SQLite field or in the code.

Is a combo box the best way to go here and have the text field hidden and have to do the manipulation in the background or is there another way I do not know of.

I see in the combo box it would work is they are integers but not text.

Thank you
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Is a combo box the best way to go here and have the text field

Yes is the best way and... no it is not a good practice to have the textfield hidden.

I see in the combo box it would work is they are integers but not text.

this is not true, text and even nodes can be handled by the combobox easily.

there are several ways to retreive the information of the combobox. I give you two:

the easiest one:
B4X:
dim item as string = combobox1.items.get(combobox.selectedindex) 'stored text value of the combobox

a more complex one but with access to more than one value

B4X:
dim arr() as string = array as string("I", "Am", "A", "Row")

dim l as list
l.add(arr) 'just an example, here you can load a lot of rows

for each arr2 as string in l
combobox1.items.add(arr(2)) 'You only show one value in the combobox
next

combobox1.tag = l 'store the list in the combobox tag

'To get the information

dim l2 as list = combobox1.tag
dim arr() as string = l2.get(combobox1.selectedindex) 'retreive list information based on the combobox index
log(arr(0)) ' and now we can access 4 values in the same row
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Yes is the best way and... no it is not a good practice to have the textfield hidden.



this is not true, text and even nodes can be handled by the combobox easily.

there are several ways to retreive the information of the combobox. I give you two:

the easiest one:
B4X:
dim item as string = combobox1.items.get(combobox.selectedindex) 'stored text value of the combobox

a more complex one but with access to more than one value

B4X:
dim arr() as string = array as string("I", "Am", "A", "Row")

dim l as list
l.add(arr) 'just an example, here you can load a lot of rows

for each arr2 as string in l
combobox1.items.add(arr(2)) 'You only show one value in the combobox
next

combobox1.tag = l 'store the list in the combobox tag

'To get the information

dim l2 as list = combobox1.tag
dim arr() as string = l2.get(combobox1.selectedindex) 'retreive list information based on the combobox index
log(arr(0)) ' and now we can access 4 values in the same row

I tried your code to solve the problem but I get

java.lang.ClassCastException: anywheresoftware.b4a.objects.collections.Map$MyMap cannot be cast to java.util.List

Here is my code with your mods. The Log does give all the correct info

B4X:
Sub btnSave_MouseClicked (EventData As MouseEvent)
   
    Log(tfdBarcode.Text)
    Log(tfdName.Text)
    Log(tfdSku.Text)
    Log(ComboBox1.Value)
    Log(cbxCut.Value)
       
   
   
    Dim item As String
    Dim maplist As Map
    maplist.Initialize
    maplist.Put("barc",tfdBarcode.Text)
    maplist.Put("sku",tfdSku.Text)
    maplist.Put("name",tfdName.Text)
    item=ComboBox1.items.get(ComboBox1.selectedindex)
    maplist.Put("supplier_ref", item )
    item=cbxCut.items.get(cbxCut.selectedindex)
    maplist.Put("cut", item )
    If DB.sql1.IsInitialized = False Then
        DB.sql1.InitializeSQLite(File.DirApp, "data/diamonds.db", True)
    End If
    DBUTILS.InsertMaps(DB.sql1,"Stock", maplist)
   
   
End Sub
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
In which line is giving you the error?

I added all the fields now just incase that was a problem. I also checked through a 3rd party software and I can add record just with the barc field

B4X:
Dim item As String
    Dim maplist As Map
    maplist.Initialize
    maplist.Put("barc",tfdBarcode.Text)
    maplist.Put("sku",tfdSku.Text)
    maplist.Put("name",tfdName.Text)
    item=ComboBox1.items.get(ComboBox1.selectedindex)
    maplist.Put("supplier_ref", item )
    maplist.Put("carat",tfdCarat.Text)
   
    item=cbxCut.items.get(cbxCut.selectedindex)
    maplist.Put("cut", item )
   
    item=cbxClarity.items.get(cbxClarity.selectedindex)
    maplist.Put("clarity", item )
   
    item=cbxPolish.items.get(cbxPolish.selectedindex)
    maplist.Put("polish", item )
   
    item=cbxColour.items.get(cbxColour.selectedindex)
    maplist.Put("colour", item )
   
    item=cbxShape.items.get(cbxShape.selectedindex)
    maplist.Put("shape", item )
   
    item=cbxGirdle.items.get(cbxGirdle.selectedindex)
    maplist.Put("girdle", item )
   
    item=cbxCutlet.items.get(cbxCutlet.selectedindex)
    maplist.Put("cutlet", item )
   
    item=cbxSymmetry.items.get(cbxSymmetry.selectedindex)
    maplist.Put("symmetry", item )
   
    item=cbxFlourescence.items.get(cbxFlourescence.selectedindex)
    maplist.Put("flourescence", item )
   
    item=cbxLab.items.get(cbxLab.selectedindex)
    maplist.Put("lab", item )
   
    item=cbxSupplier.items.get(cbxSupplier.selectedindex)
    maplist.Put("supplier_no", item )
    '18
    maplist.Put("tbl",tfdTbl.Text)
    maplist.Put("depth",tfdDepth.Text)
    maplist.Put("measurement",tfdMeasurement.Text)
    maplist.Put("usd_rapp",tfdusd_rapp.Text)
    maplist.Put("zar_rapp",tfdzar_rapp.Text)
    maplist.Put("usd_cost",tfdusd_cost.Text)
    maplist.Put("sale_percent",tfdsale_percent.Text)
    maplist.Put("usd_price",tfdusd_price.Text)
    maplist.Put("zar_price",tfdzar_price.Text)
    maplist.Put("profit",tfdprofit.Text)
    maplist.Put("price",tfdprice.Text)
    maplist.Put("price_carat",tfdprice_carat.Text)
    maplist.Put("ref_no", tfdSupplierReference.Text)
    maplist.Put("fixed_cost",tfdfixed_cost.Text)
    maplist.Put("xrate",tfdxrate.Text)
    maplist.Put("xrate_sold",tfdxrate_sold.Text)
    maplist.Put("cert_file_name",tfdcert_file_name.Text)
   
   
   
   

    If DB.sql1.IsInitialized = False Then
        DB.sql1.InitializeSQLite(File.DirApp, "data/diamonds.db", True)
    End If
    DBUTILS.InsertMaps(DB.sql1,"Stock", maplist)
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
I am sorry for the delay, i was not on my computer.

the problem is that you are passing a map:

B4X:
Dim maplist As Map
 maplist.Initialize
DBUTILS.InsertMaps(DB.sql1,"Stock", maplist)

when you have tu pass a list of maps:

B4X:
Dim maplist AsMap
maplist.Initialize

dim listOfMaps as list
listofmaps.initialize

listofmaps.add(maplist)
DBUTILS.InsertMaps(DB.sql1,"Stock", listofmaps)

This is because the code lets you add more than one row at a time, each row being a new insert query.

For your case its only one, but still applies.
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
I am sorry for the delay, i was not on my computer.

the problem is that you are passing a map:

B4X:
Dim maplist As Map
maplist.Initialize
DBUTILS.InsertMaps(DB.sql1,"Stock", maplist)

when you have tu pass a list of maps:

B4X:
Dim maplist AsMap
maplist.Initialize

dim listOfMaps as list
listofmaps.initialize

listofmaps.add(maplist)
DBUTILS.InsertMaps(DB.sql1,"Stock", listofmaps)

This is because the code lets you add more than one row at a time, each row being a new insert query.

For your case its only one, but still applies.


AHHH, now I remember the Listofmaps , I copied it from another sub in the program and did not take the LIST area.

Thx
 
Upvote 0
Top