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

Discussion in 'Android Questions' started by Rob Rendle, Feb 16, 2015.

  1. Rob Rendle

    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
     
  2. NJDude

    NJDude Expert Licensed User

    You could use a variable in Process_Globals, variables declared there are accessible by any activity.
     
    lemonisdead and thedesolatesoul like this.
  3. Rob Rendle

    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 :(
     
  4. NJDude

    NJDude Expert Licensed User

    Then use SENDER, HERE is an example.
     
    lemonisdead and thedesolatesoul like this.
  5. ac9ts

    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.
     

    Attached Files:

    Peter Simpson and Rob Rendle like this.
  6. Rob Rendle

    Rob Rendle Member Licensed User


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

    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: Feb 18, 2015
  8. mangojack

    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 ...
    Code:
    Public CheckText As String
    Then in the checkbox event ...
    Code:
    Sub chkPig_CheckedChange(Checked As Boolean)
      CheckText = chkPig.Text 
      
    StartActivity(Activity2)
    End Sub

    or better still .. set all your CheckBoxes to the same event ...
    Code:
    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 ...
    Code:
    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: Feb 19, 2015
  9. Reviewnow

    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

    Code:
    Sub CheckToMap(Panel As PanelAs 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
     
    thedesolatesoul likes this.
  10. Rob Rendle

    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.
     
  11. Reviewnow

    Reviewnow Active Member Licensed User

    Yes that is correct
     
  12. Rob Rendle

    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 :-

    Code:
    Sub CheckToMap (Panel As PanelAs 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 :-

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

    End Sub
    DB Query :-

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

    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 :(
     
  14. Reviewnow

    Reviewnow Active Member Licensed User

    Rob can you upload your code ?
     
    Rob Rendle likes this.
  15. Rob Rendle

    Rob Rendle Member Licensed User

    Thanks - I have sent a link to you in the conversation :)
     
  16. Reviewnow

    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: Feb 24, 2015
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice