¿La casilla de verificación está marcada? Bucle dentro de los elementos de la casilla de verificación

int count = listView.getChildCount();
for (int i = 0; i < count; i++) {
    View child = list.getChildAt(i);
    //check that child..
}

I wanted to use the following code to see if the total number of checkBox(es) were checked or not. Like if I have 3 checkBoxes I would want something equivalent to:

if(!list.getChildAt(0) && !list.getChildAt(1) && !list.getChildAt(2)){
       // do something with all unchecked checkbox
}

How do I loop through like this, because for one I am not sure about the number of contents in my checkbox.

preguntado el 04 de septiembre de 13 a las 02:09

you are checking all checkbox are unchecked or not -

3 Respuestas

Just modify the if statement to return the state of the checkbox.

int count = listView.getChildCount();
boolean allUnchecked = true;
for (int i = 0; i < count; i++) {
    Object child = (Object) listView.getChildAt(i);
    if (child instanceof CheckBox) {
        CheckBox checkBoxChild = (CheckBox) child;
        if (checkBoxChild.isChecked()) {
            allUnchecked = false;   //one is checked, sufficient to say that not all is unchecked
            break;    //get out the for loop
        }
    }
}

allUnchecked will be true if all checkBoxes are unchecked, false otherwise

I'm not an Android developer and I can't find the docs for getChildAt so I don't know what it returns. If its an Object you can omit the cast.

Its good to check for null regreso del getChildAt

Ps: this is not a good code, you can take it like a pseudo-code to know how to implement the logic of how is checked or not, getting the list of the CheckBoxes is your task :)

Respondido el 04 de Septiembre de 13 a las 05:09

please check edited question ... i meant to say if all the checkboxes were unchecked. - Rakeeb Rajbhandari

@user2247689 it's that? - Diego C Nascimento

cannot call getChildAt(0) on child.getChildAt(0).isChecked() - Rakeeb Rajbhandari

obteniendo el siguiente error: 09-04 07:48:02.992: E/AndroidRuntime(9000): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.CheckBox - Rakeeb Rajbhandari

Error is clear. It says that you have wrong casting. You are trying to get object as CheckBox which is RelativeLayout. - Solo trabajo

you can use isChecked function to find if the checkbox is checked or not and then build your logic on that.

I would be able to put the logic if you can clarify your question further.

Respondido el 04 de Septiembre de 13 a las 02:09

if(!list.getChildAt(0) && !list.getChildAt(1) && !list.getChildAt(2)){
       // do something with all unchecked checkbox
}

en lugar de

if(!list.getChildAt(0) || !list.getChildAt(1) || !list.getChildAt(2)){
       // do something with all unchecked checkbox
}

Respondido el 04 de Septiembre de 13 a las 02:09

Hmmm how do i get the count of checkboxes ? - Rakeeb Rajbhandari

You are trying to get the result count - newuser

yes very much ... for one i am unsure of the number of checkboxes that will appear ... and after that i want to make sure that all of the checkboxes are unchecked. - Rakeeb Rajbhandari

You can assign false to all the checkboxes - newuser

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.