Android Question Multiple Checkboxes - Collect which is checked and pass to new Activity

Rob Rendle

Member
Licensed User
Sorry, so many newb questions :(. I'm struggling to find the answer to this one, or an example that I can understand.


I have several checkboxes, I want to collect which checkbox(es) are checked and pass them to a new Activity.


The new activity bit is straight forward so don't need any help with that, however, this is where I'm struggling.

Say I have 4 checkboxes :-

Cat
Dog
Fish
Pig

I want to see which of those has been checked (so for example, Cat and Dog are checked) and then pass Cat,Dog to a new activity.

Thanks (and sorry for making your eyes bleed, again, with another 'silly' question)

Rob
 

Rob Rendle

Member
Licensed User
Thanks NJ, that's the bit I do understand :), it's getting all of the selections to a value that's stumping me (It's been a long day).

When you create the checkboxes and create their event, you end up with 4 events, it's gathering the result of each event in one go and sending it to the value :(
 

ac9ts

Active Member
Licensed User
If I understand, you have the 4 check boxes on an activity and you want to read the state of them and send the value to a different activity?

As NJDude mentioned, create a variable in Process Globals. To keep things straight in your head, make 4 Boolean values named cat, dog, fish, and pig. In whatever section of code you use to start the 2nd activity, read the status of the check box and set the variables (cat = Checkbox1.Checked). In the 2nd activity, you can "see" the values.

You really don't need to create an event for the check boxes unless you are going to do something when the user changes the values.

Attached is a simple demo that does what I tried to explain.
 

Attachments

Rob Rendle

Member
Licensed User
If I understand, you have the 4 check boxes on an activity and you want to read the state of them and send the value to a different activity?

As NJDude mentioned, create a variable in Process Globals. To keep things straight in your head, make 4 Boolean values named cat, dog, fish, and pig. In whatever section of code you use to start the 2nd activity, read the status of the check box and set the variables (cat = Checkbox1.Checked). In the 2nd activity, you can "see" the values.

You really don't need to create an event for the check boxes unless you are going to do something when the user changes the values.

Attached is a simple demo that does what I tried to explain.

Thank you for the clear description and example, it's much appreciated.
 

Rob Rendle

Member
Licensed User
Ok so this is where it's getting a bit tricky.

Ac9ts - your example works perfectly. I can see that working just fine in my code (I can't thank you enough, for something so simple, I'm surprised how I didn't spot it!)

I need to pass the selected checkboxes to an SQL query,

so for example, my query would be something like

"SELECT Names From Rec WHERE Animals ='" & ActivityName.chkboxID & "'"

Where the chkboxID would equal the results we found using your example (i.e Cat, Dog, Fish)

Usually, you'd have a simple line like :-

chkboxID = Value

Which doesn't work in this example, as setting a Value as an object on the Click event causes an error (and doesn't pass the results anyway)

I'm guessing I need to figure out a way of turning the chkCat.Checked result to a String that is simply Cat

Do you have any more marvelous pointers? Many thanks in advance


Rob
 
Last edited:

mangojack

Well-Known Member
Licensed User
*Edit .. re reading initial post I realize you wish to pass values of multiple checked boxes so the below code will probably not suit you needs ...........

One option is to set a Process Global in your main activity ...
B4X:
Public CheckText As String
Then in the checkbox event ...
B4X:
Sub chkPig_CheckedChange(Checked As Boolean)
  CheckText = chkPig.Text 
  StartActivity(Activity2)
End Sub

or better still .. set all your CheckBoxes to the same event ...
B4X:
 Sub chkBox_CheckedChange(Checked As Boolean)
  Dim chk As CheckBox = Sender
  CheckText = chk.Text
  StartActivity(Activity2)
End Sub
Then from the second activity you can handle the SQL query ...
B4X:
Dim query As String
query = "SELECT Names From Rec WHERE Animals = ?"
SQL1.ExecQuery2(query,Array As String(Main.CheckText))
From what I can gather you are starting your second activity following a single checkbox click .
 
Last edited:

Reviewnow

Active Member
Licensed User
Give each of the checkbox tags a value as example
chkBox1.Tag = "C_Dog" or C_YourCheckBoxID
the following code assumes all your checkboxes are on a single panel

How To Use

Dim m1 as map
m1.Initialize
m1 = CheckToMap(Panel1)
'Now all your checked True Values are available in the Map

B4X:
Sub CheckToMap(Panel As Panel) As Map
Dim m As Map
m.Initialize
       
For i = 0 To Panel.NumberOfViews - 1

    If Panel.GetView(i).IsInitialized  Then
        Dim chkbox As CheckBox
        Dim rvalue As String
        rvalue = Panel.GetView(i).tag
        If rvalue.Contains("C_") Then
            chkbox = Panel.GetView(i)       
            If chkbox.Checked = True Then
               Dim chkName As String = Regex.Split("_",rvalue)(1)
                m.Put(chkName, "True")
            End If
        End If

    End If
Next
Return m
End Sub
 

Rob Rendle

Member
Licensed User
Thanks for that, the solution looks like it will work. I appreciate you taking the time to knock up an example.

In reference to m.put line, will the chkName value be the tag for the checkboxes that are true? So, in that instance to then use the results to execute an SQL query, I'd need to make sure the tag is Cat, Dog etc as those are the values in the db.
 

Rob Rendle

Member
Licensed User
Unfortunately, this doesn't seem to be working.

In my SQLite db, I have a new column called CI, with the values of C_Cat, C_Dog and so on. The tag of each Checkbox is C_Cat, C_Dog and so on.

However whenever I run the query based on the m1 map, I get no results at all, in fact I can't even tell if m1 is getting the right value.

Here is the code :-

B4X:
Sub CheckToMap (Panel As Panel) As Map

Dim m As Map


m.Initialize

For i = 0 To Panel.NumberOfViews -1
    If Panel.GetView(i).IsInitialized Then
        Dim chkbox As CheckBox
        Dim rvalue As String
    If rvalue.Contains("C_") Then
        chkbox = Panel.GetView(i)
        If chkbox.Checked = True Then
       
        Dim chkName As String = Regex.Split("_",rvalue)(1)
        m.Put(chkName, "True")
       
       
       
    End If
    End If
    End If
    Next
   
    Return m

End Sub
Then the button press :-

B4X:
Sub Gen_Click
    CheckToMap(chkPanel)
    m1 = CheckToMap(chkPanel)
   
    StartActivity(SmartSearchResults)

End Sub
DB Query :-

B4X:
"SELECT Name FROM Rec WHERE CI = '" & SmartSearch.m1 & "'"
Note that m1 is declared as a Map on Process_Globals
 

Rob Rendle

Member
Licensed User
So I've been working on this a little more today and with the aid of Log, I can see that the following

chkName, always equals Null, no matter what
m1 (and m) always return :-

(MyMap) {}

Grumble :(
 

Reviewnow

Active Member
Licensed User
the error was a mistake you where missing a statement in your code
Dim chkbox AsCheckBox
Dim rvalue AsString = Panel.GetView(i).Tag '<<<< this was the error forgot to look for the tag

Also look at the code in
SListViewFill
 
Last edited:
Top