Android Question How do I loop through checkboxes on a layout in a TabHost?

Steve Miller

Active Member
Licensed User
Longtime User
I have a Tabhost with 10 tabs. On each tab I have a panel that contains different controls on them. I do it this way because it's easier to center the panel in the app. On tab 6 the panel contains 24 checkboxes. How can I loop through these checkboxes to see if any of them are checked? Also, is it possible to determine which are checked?
 

stevel05

Expert
Licensed User
Longtime User
Something like this should do it, I haven't tested it as I don't have a similar project. To keep track of which items are selected you'll need to use the Tag field.

B4X:
        Dim JO As JavaObject = TabHostName
        For i = 0 To JO.RunMethod("getChildCount",Null)-1
            Dim V As View = JO.RunMethod("getChildAt",Array As Object(i))
            If V is CheckBox Then
              Dim CB As CheckBox = V
              If CB.Selected Then
                  If CB.Tag = "1" Then
                      'Do whatever
                  End if
              End If
            End If
        Next

Alternatively, you could use the CheckBox callback routine and monitor the checkboxes directly. If CBArray() is a global variable and you have numbered the tags 0-however many.

CB is the eventname you've given to all of the checkboxes you want to monitor.
B4X:
Sub  CB_CheckedChange(Checked As Boolean)
      Dim CBox As CheckBox = Sender
      CBArray(CBox.Tag) = Checked
End Sub

Of a combination of the two.
 
Last edited:
Upvote 0

Steve Miller

Active Member
Licensed User
Longtime User
I guess I'm doing something wrong. I don't see how I set this to only loop through the checkboxes located on the panel that is in Tab 6.
Also, it's checking for the checkbox tag to equal 1, but how/where is this being set?

When it goes through the first time, and it interrogates the "If V is Checkbox then" it is showing V (LinearLayout): Layout not available. Then it drops out of the loop and exits the sub.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Sorry, I copied the wrong subroutine from one of my progs. The attached file has a working setup (I tested it this time). You'll need to set up the tags, however you created the views, panels and checkboxes will need to be tagged.
 

Attachments

  • tht.zip
    7.6 KB · Views: 217
Upvote 0
Top