How to show a dialog with check predefined markings?

0

Good morning, I have the following Dialog with Multiple Checkboxes Selection List, How can I make it so that when I call the Dialogue, I show myself with check some items? for example, if I want to load with check item 5 and item 9, how can I do?

public AlertDialog listDialogHimnos() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final String[] listitems = ListaHimnos.arreglo.tokens;
    builder.setTitle("Himnos 1 - 371")
            .setMultiChoiceItems(listitems, null, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    if (isChecked) {
                        //Si hay check
                    } else {
                        //Si se quita el check
                    }
                }
            });

    return builder.create();
}
    
asked by Saúl Hernández 14.05.2018 в 18:38
source

2 answers

0

I have solved my question! First of all I have declared my list of the dialog as a global variable, at the same time, I have declared another Boolean type variable of the size of my list in which a list of the status of the items with check is stored.

final String[] listitems = ListaHimnos.arreglo.tokens;
final boolean bl[] = new boolean[listitems.length];

The dialogue is as follows:

public AlertDialog listDialogHimnos() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Himnos 1 - 371")
            .setMultiChoiceItems(listitems, bl, new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    if (isChecked) {
                        //Si hay check
                        bl[which] =true;//Guardo el estado en esta posicion
                    } else {
                        //Si se quita el check
                        bl[which] = false;//Elimino el estado de esta posicion
                    }
                }
            });
    return builder.create();
}

As you can see in

.setMultiChoiceItems(listitems, bl,.........

listitems is the list that shows me in the dialog
bl is the boolean variable with the states of the items

    
answered by 15.05.2018 / 18:41
source
0

With setChecked in this way:

tuCheckBox5.setChecked(true);
tuCheckBox9.setChecked(true);
    
answered by 15.05.2018 в 02:59