-1

I have 25 checkbox on the xml. I want to know how many of the boxes in the document are checked. tried to do looping like this..

  p1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (p1.isChecked()){
            palm1=longitude+","+latitude;
            //Toast.makeText(getBaseContext(), palm1, Toast.LENGTH_SHORT).show();
        }else {
            palm1="health";
            //Toast.makeText(getBaseContext(), "uncheck", Toast.LENGTH_SHORT).show();
        }

        percentage();

            }});  




public void percentage(){
        Vector<CheckBox> allCheck=new Vector<CheckBox>();
        int counterChecked=0;

        allCheck.add(p1);
        allCheck.add(p2);
        allCheck.add(p3);
        allCheck.add(p4);
        ................
        allCheck.add(p25);


        for(int i=0; i<allCheck.size();i++)
               if(allCheck.get(i).isChecked()){
               counterChecked++;
               }

        double percent = (counterChecked * 100) / allCheck.size();
        persen=String.format("%.1f %%", percent);
        kerusakan= Integer.toString(counterChecked);
 Toast.makeText(getBaseContext(),percent, Toast.LENGTH_SHORT).show();

        Toast.makeText(getBaseContext(), kerusakan, Toast.LENGTH_SHORT).show();


    }

but it says the 0 values. How can I find out how many boxes are checked?

Sa Sya
  • 21
  • 6

2 Answers2

0

I would recommend having a counter.When the user click on a check box then you will check if it will become checked or unchecked.In the first case you will do counter++; and in the second case counter--;. But you should show us a sample of your code for a more specific answer.

PL13
  • 393
  • 2
  • 5
  • 13
  • This implies that you need a listener for each checkbox (maybe a shared listener) – user2340612 Dec 03 '15 at 01:25
  • Yes there should be a listener for each checkbox here. – PL13 Dec 03 '15 at 01:26
  • @user2340612 i've used a listener for each checkboxes, but still cant do counter++.. it give me 0 for the output. – Sa Sya Dec 03 '15 at 01:28
  • In the code you posted i see that you have not assigned your checkboxes.Inside the onCreate method you should do something like : cb = ( CheckBox ) findViewById( R.id.cb ); for each checkbox – PL13 Dec 03 '15 at 01:31
0

You would save code and time by implementing the OnCheckChangeListener to your whole activity. This should trigger any click of a checkbox then you can create your count routine on the onCheckChanged method implementation.

Something like this:

public class YourActivity extends Activity implements OnCheckedChangeListener{

... declare a global array here

private CheckBox[] checkboxes;

.... somewhere in the onCreate method

    checkboxes = new CheckBox[3];

    for (int i = 0; i < checkboxes.length; i++) {
            String checkboxID = "checkBox" + (i + 1);

            int resID = getResources().getIdentifier(checkboxID, "id",
                    getPackageName());
            checkboxes[i] = ((CheckBox) findViewById(resID));
            checkboxes[i].setOnCheckedChangeListener(this);
            allCheckBoxes.add(checkboxes[i]);
    }

... and the override method for your interface

    @Override
  public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    int countOfCheckBoxesChecked = 0;
    for (CheckBox checkbox : allCheckBoxes) {
      if (checkbox.isChecked()) {
        countOfCheckBoxesChecked++;
      }
    }
    Log.i(TAG, "number of checked = " + countOfCheckBoxesChecked);

  }
Marka A
  • 258
  • 5
  • 15
  • Could update this to use `getResources().getIdentifier()`; instead of typing each line, you could loop over all the checkboxes - see here for example http://stackoverflow.com/a/17203591/2308683 – OneCricketeer Dec 03 '15 at 02:16
  • Thanks for your comment @cricket_007 ... have edited sample code to use looping and make it all DRY... – Marka A Dec 03 '15 at 02:47
  • @SaSya - You really don't even need the array... Just use `allCheckBoxes` – OneCricketeer Dec 03 '15 at 03:41