B4J Question Creating a list of Checkboxes

dieterp

Active Member
Licensed User
Longtime User
I need to create a list of checkboxes with labels so that a user can select various items from the list and when clicking save, the app will then iterate through the list and insert all checked values into a table (I.e. emulate a b4a InputMap). I've gone through the one tutorial listed in the forum but when I click on one of the checkboxes in the list it doesn't fire an event (You need to click on the actual row in the list to fire the SelectedIndexChanged event.

What is the best way to approach this scenario?
 

stevel05

Expert
Licensed User
Longtime User
Can you show the code, or better still upload your project (File /Export As Zip) so we can see what you've done and how it's set up.
 
Upvote 0

dieterp

Active Member
Licensed User
Longtime User
Below is the code. So what I need to understand is how I trap the check event when a user clicks on a checkbox, or how I would iterate through the list when a user clicks on a button to Save the items that are checked. With the code below you need to click on the list row to fire the lv_SelectedIndexChanged event

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
   
    Private lv As ListView
   
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    MainForm.Show
   
    lv.Initialize("lv")
    MainForm.RootPane.AddNode(lv, 0, 0, 0, 0)
    MainForm.RootPane.SetAnchors(lv, 0, 0, 0,0)
   
    For i = 0 To 5
       
        Dim chk1 As CheckBox
        chk1.Initialize("")
        chk1.Text = "Test Check " & i
        lv.Items.Add(chk1)
       
    Next
   
End Sub

Sub lv_SelectedIndexChanged(Index As Int)
    If Index > -1 Then
        Dim chk1 As CheckBox = lv.SelectedItem
        Log(chk1.Text)
    End If
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
To get the Checkbox changed event you need to set the eventname on the checkbox, then add a sub to catch the event.

Initialize as
B4X:
chk1.Initialize("chk1")

Add Sub
B4X:
Sub chk1_CheckedChange(Checked As Boolean)
    Dim CB As CheckBox = Sender
    Log(CB.Text & " : " CB.Checked)
End Sub

To iterate over the checkboxes:
B4X:
For Each CB As Checkbox in lv.Items
    Log(CB.Text & " : " & CB.Checked)
    If CB.Checked Then
        'Do Whatever
    End If
Next
 
Upvote 0
Top