Android Question Add button to a panel from code [Solved]

Colin Evans

Active Member
Licensed User
Longtime User
Hi, is it possible to add a button to a panel from code?

I use the following to add data from a database, which works well and every button is correctly named during the build but when you select any button from the various panels added they all refer to the last one entered, i.e. if I have 11 entries, Qty = 11 and I click on any of the buttons that are in the form (from Designer) I get the text and tag = 11, and if it is possible how would I create the button_click so it caters for all the entries as this would be variable

I need the button so I can click on it to edit the relevant entry in the database, using the ID held

Many thanks in anticipation :)

B4X:
    If Qty=0 Then
        ToastMessageShow("No data", False)
        btnFirstSave.Visible = True
    Else
        Dim i As Int
        MySql="SELECT * FROM medication"
        cursor1 = SQL1.ExecQuery(MySql)
        btnFirstSave.Visible = False
        For i =0 To Qty-1
            Dim footer As B4XView = xui.CreatePanel("")
            footer.SetLayoutAnimated(0,0,0,100%x, 250dip)
            footer.LoadLayout("medicines")
            CustomListView1.Add(footer,"")
            cursor1.Position = i
                btnID.Text = cursor1.GetString("id")
                btnID.Tag = cursor1.GetString("id")
                lblMedication.Text = cursor1.Getstring("drug")
                lblDose.Text = cursor1.GetString("dose")
                lblIssued.Text = cursor1.GetString("dateissued")
                lblNext.Text = cursor1.GetString("lastissue")
                lblDirections.Text = cursor1.GetString("directions")
        Next
        btnFirstSave.Visible = False
    End If
End Sub

Sub btnID_Click
    Log(btnID.Tag & " - " & btnID.Text)
End Sub
 

Colin Evans

Active Member
Licensed User
Longtime User
So btnID is in "medicines" layout and it's event name is btnID, right?
Have you declared it in Globals (or Class_Globals if it is a class)?
Hi Mario, it's declared in Globals via Designer
and each panel that contains the data for each medication shows the correct ID displayed on the button but as I said which ever button you select it returns the value of the last button allocated in the code, so I thought it may be better to remove from the layout and create, if possible a unique button in the for next loop, any suggestions greatly received
 
Upvote 0
Top