Android Question Detect how many checkboxes are checked in CustomView

trueboss323

Active Member
Licensed User
Longtime User
On a CustomView, this is the code for creating a new list item:

B4X:
Sub CreateListItem(Text As String, Text2 As String, Width As Int, Height As Int, Image As Bitmap) As Panel
        Dim p As Panel
    Dim chk As CheckBox
    p.Initialize("")
    p.Color = Colors.Black
    Dim img1 As ImageView
    img1.Initialize("img1")
    img1.Gravity = Gravity.FILL
    img1.Bitmap = Image
   
    chk.Initialize("chk")
   
    title.Initialize("")
    title.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.LEFT)
    title.Text = Text
    title.TextSize = 16
    title.TextColor = Colors.White
    Dim subtitle As Label
    subtitle.Initialize("")
    subtitle.Gravity = Bit.OR(Gravity.CENTER_VERTICAL, Gravity.BOTTOM)
    subtitle.Text = Text2
    subtitle.TextSize = 14
    subtitle.TextColor = Colors.White
    p.AddView(img1, 2dip, 2dip, 50dip, Height - 4dip) 'view #0

    p.AddView(title, 65dip, -7dip, 300dip, Height - 4dip) 'view #1
    p.AddView(subtitle, 66dip, -2dip, 300dip, Height - 4dip) 'view #1
    p.AddView(chk, 88%x, 2dip, 50dip, Height - 4dip) 'view #2

    Return p
End Sub

And to insert it, I use this:

B4X:
    CustView.Add(CreateListItem("Title","Subtitle", CustView.AsView.Width, 50dip, LoadBitmap(File.DirAssets, "image.png")), 50dip, "")

Using this, is there a way to determine how many checkboxes are selected within the list?
 

DonManfred

Expert
Licensed User
Longtime User
Sub CreateListItem(Text As String, Text2 As String, Width As Int, Height As Int, Image As Bitmap) As Panel
Dim p As Panel
Dim chk As CheckBox
i suggest to put a reference to a global list to all your checkbox instances.

You then can use this list to iterate through all you checkbox to get the count of checked checkboxes.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Create a process global variable (List) to hold them

B4X:
dim mycheckboxes as list
in process global of main or the new starter service

B4X:
mycheckboxes.initialize
in activity create (or service create of the starter service)

And then you can CLEAR the content of the list when building your customlistview-layout (start with clearing the list
B4X:
mycheckboxes.clear
)

Then, in your CreateListItem sub

B4X:
Sub CreateListItem(Text As String, Text2 As String, Width As Int, Height As Int, Image As Bitmap) As Panel
        Dim p As Panel
    Dim chk As CheckBox
    p.Initialize("")
    p.Color = Colors.Black
    Dim img1 As ImageView
    img1.Initialize("img1")
    img1.Gravity = Gravity.FILL
    img1.Bitmap = Image
 
    chk.Initialize("chk")
    mycheckboxes.Add(chk)  ' Put a reference to the checkbox to the global list

At any time you need to get the count of checked checkboxes you then iterate through your global list
- get the checkbox
- check if it is checked or not
- count the checked ones

For example:

B4X:
    Dim count As Int = 0
    For i = 0 To mycheckboxes.Size-1
        Dim chk As CheckBox = mycheckboxes.Get(i)
        If chk.Checked Then count = count +1
    Next
    Log(count&" Checkboxes are checked...")
 
Upvote 0
Top