Android Question Database Query to Button array

h725

Active Member
Licensed User
Longtime User
I need to fill a button array with database results.
E.g. Database table "products" with :

ProductID Product
1 Egg
2 Ham
3 Coke

In VB it is easy to loop through a datatable,
generate a new object (e.g. button) and
fill a flowlayoutpanel.

I would like to try a same thing on B4A.
I have found a sample from the TingTong Game to generate
a button array:

  1. Dim width, offsetX, offsetY As Int
  2. width = 100dip
  3. offsetX = (100%x - width * 3 - 10dip * 2) / 2
  4. offsetY = (100%y - width * 3 - 10dip * 2) / 2
  5. For x = 0 To 2
  6. For y = 0 To 3
  7. Dim b As Button
  8. b.Initialize("button") 'All buttons share the same event sub
  9. b.TextSize = 30
  10. ' b.Text = "test"

  11. ' Activity.AddView(b,offsetX + x * (width + 10dip), offsetY + y * (width + 10dip), width, width)
  12. Activity.AddView(b,offsetX + x * (width + 5dip), offsetY + y * (width + 5dip), width, width)
  13. ProdButtons(x, y) = b 'store a reference to this view
  14. Next
  15. Next
Number of buttons have to be declared in the Sub Globals. For the example it should be
  1. dim prodbuttons(3,4) as buttons
Maybe somebody has an example how to dynamically generate the number of buttons for
the array out of the number of tablerows, with adding the column "product" as buttontext
and adding the column "ProductID" as buttontag.


Can anybody help me ?
 

DonManfred

Expert
Licensed User
Longtime User
Instead of using a multidimensional array you can simply use a list to store all objectreferences.
Maybe better a List of Maps.

something like

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim btnList As List
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    btnList.Initialize
  
  Dim width, offsetX, offsetY As Int
  width = 100dip
  offsetX = (100%x - width * 3 - 10dip * 2) / 2
  offsetY = (100%y - width * 3 - 10dip * 2) / 2
  For x = 0 To 2
    For y = 0 To 3
        Dim b As Button
        b.Initialize("button") 'All buttons share the same event sub
        b.TextSize = 30
        ' b.Text = "test"

        ' Activity.AddView(b,offsetX + x * (width + 10dip), offsetY + y * (width + 10dip), width, width)
        Activity.AddView(b,offsetX + x * (width + 5dip), offsetY + y * (width + 5dip), width, width)
        b.Text = "value from 'product'"
        b.Tag = "value from 'ProductID'"
            Dim m As Map ' Dim map to be used for each button. The map(s) are added to the list
            m.Initialize
            m.Put("x",x) ' Store x and y value in the Map
            m.Put("y",y)
            m.Put("button",b) 'store a reference to this view
            btnList.Add(m) ' Add the map to the list
            'ProdButtons(x, y) = b 'store a reference to this view
    Next
  Next

End Sub

Sub button_Click
    Dim btn As Button = Sender
    For i = 0 To btnList.Size-1
        Dim m As Map = btnList.Get(i)
        If btn = m.Get("button") Then
            Log($"ButtonClicked(${m.Get("x")},${m.Get("y")})"$)
        End If
    Next
  
End Sub


You then have a list with all references. See button_click code to iterate through all buttons in the list to find the clicked one

The example dont uses a database because i do not much with databases (sqlite) in android... But my code should be easy to adapt to your needs...
 
Upvote 0

h725

Active Member
Licensed User
Longtime User
Thanks very much for the answer.
I will try to get into details:
If I know that I have e.g. 100 datatable entries (rows), how can I set the
tag and text property of the button directly in the loop ?
e.g.

Loop of the button-array:
  1. For x = 0 To 4
  2. For y = 0 To 19
  3. Next
  4. Next

Possible loop of the "database-table-request":
  1. For Each records() As Object In result.rows
  2. productid = records(0)
  3. productname = records(1)
  4. ...

Now I want to set the text of the button to productname
and the tag of the button to the productid
 
Upvote 0
Top