Android Question Generate dynamic buttons and fill from database values

Bapha

Member
Hello friends, how can we fill the dynamic buttons text from sql database values? For example we have got values (milk, chocolate, orange, melon ...) in database column, how can we generate (horizontal rows and vertical rows) dynamic buttons text with database values.
Getting from database:
Dim Cursor As JdbcResultSet
Cursor =connect.sql1.ExecQuery("select product from stock order by product asc")
Cursor.Close

This one is generates dynamic buttons (horizontal rows and vertical rows) from MarkusR thank you.
For x = 0 To 3
For y = 0 To 10
Dim btn1 As Button
Dim PosX As Pos
PosX.x = x
PosX.y = y
btn1.Initialize("Button")
btn1.Text=**product name**
ScrollView1.Panel.AddView(btn1,x*25%x,y*15%y,100dip,100dip)
Next
Next
How can we combine properly these two different cases same like the picture
dynamic button products.jpg


Thanks in advance
 

MicroDrie

Well-Known Member
Licensed User
First it helps to better read you program code if you first hit the code tag </> and put the code into the code field.

I think you should take a look at the [B4X] [DSE] Designer Script Extensions. The program example DSExampleNumpad solve the different screen sizes resolution problem for you. You need to use the new IDE version 10.80 BETA .

You can a idee for set and get the button text based on this example which I use in the DSExampleToolbar example:

Designer Script Extensions:
    Log(Pane1.NumberOfViews)
    Log(Pane1.GetView(1))
    Log(Pane1.GetView(1).Text)

'    --- Hide the first button    
    Dim p As B4XView = Pane1.GetView(0)
    p.Visible = False
    
'    --- Update the text include font awesome world icon
    Pane1.GetView(2).Text = $"${Chr(0xF0AC)} Apple"$
    
'    --- update the screen display
    Sleep(100)
    
'    --- check the update
    Dim p As B4XView = Pane1.GetView(1)
    Log(p.Text)
        
'    --- turn on the first button  
    Dim p As B4XView = Pane1.GetView(0)
    p.Visible = True
        
'    --- update the screen display
    Sleep(100)

This is all still very new and I am also exploring the new possibilities. Since I see that you are a new user I recommend that you follow Erel's advice and develop and test the program in B4J IDE first as a B4XPages solution. Because it is written in B4X, you can easily use it in B4A IDE once tested.
 
Upvote 0

Bapha

Member
I mean get product names from database and fill dynamic generated buttons text with this products names. How can we perform same like the picture.
Thanks in advance.
 

Attachments

  • Dynamic generated buttons fill form sql.jpg
    Dynamic generated buttons fill form sql.jpg
    26.6 KB · Views: 104
Upvote 0

MicroDrie

Well-Known Member
Licensed User
The forum will help you with your program issues, however we will not make a program for you because we don't know what you want.
So export you current program to a zip file and post it here.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I mean get product names from database and fill dynamic generated buttons text with this products names.

Here is something that might get you going - it is not a complete solution. I don't know how much coding you have done before so I have kept it very simple. If you are just starting out then you should use B4XPages - I have not done so and there is more work to be done to handle the app returning from the background. But this example does demonstrate putting a variable number of names into an array of buttons.

Also you should use the Designer if possible to draw the button array, although doing it in code might be better if you need different numbers of buttons for different data sets.
 

Attachments

  • Buttons.zip
    9.8 KB · Views: 101
Upvote 0

PaulMeuris

Active Member
Licensed User
There are alternative ways...
Why not loop through the records from the resultset and create and add a button then to the scrollview?
Here's a code teaser:
looping through a resultset:
    Do While rs.NextRow
        Private btnproduct As Button = set_button(rs.GetString("rowid"),rs.GetString("name"))
        ScrollView1.Panel.AddView(btnproduct,leftpos,toppos,btnproduct.Width,btnproduct.Height)
        ...
    Loop
    ScrollView1.Panel.Height = cnt*btnproduct.Height + 10dip
Also note that you can use panels or labels with a click event to accomplish the same result.
 
Upvote 0
Top