dimanche 23 août 2015

Disable AlertDialog PositiveButton If No CheckBox is Checked

I've got the following code:

final AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
builder.setTitle(R.string.str_dbox_remove_plant_title);
final HashMap<String, Integer> stuff = myCollection.getListOfStuffWithLocation();
final CharSequence[] items = stuff.keySet().toArray(new CharSequence[stuff.size()]);
final List<Integer> indexesOfSelectedStuff = new ArrayList<Integer>();
builder.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        if (isChecked)
            indexesOfSelectedStuff.add(stuff.get(items[which].toString()));
        else if (indexesOfSelectedStuff.contains(stuff.get(items[which].toString())))
            indexesOfSelectedStuff.remove(which);
    }
});
builder.setPositiveButton(R.string.str_dbox_remove_stuff_ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        for (int i = 0; i < indexesOfSelectedStuff.size(); i++) {
            myCollection.removeStuff(i);
        }
    }
});
builder.setNegativeButton(R.string.str_dbox_remove_stuff_cancel, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
    }
});              

builder.setCancelable(false); // so as to prevent the back button from closing the dialog box
final AlertDialog alert = builder.create();
alert.setCanceledOnTouchOutside(false);                  
alert.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); // by default, disable OK button
alert.show();

I have an AlertDialog with some CheckBoxes and I would like to disable the PositiveButton as long as there aren't any CheckBoxes ticked.

I have a List called indexesOfSelectedStuff and you could say that in some way, using its size() method will give me an idea of how many CheckBoxes are selected. However, I am not sure where to place it or how to reference the PositiveButton within the builder. I wanted to place it on the onClick method (as defined in the OnMultipleChoiceClickListener()), like so:

@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (isChecked)
        indexesOfSelectedStuff.add(stuff.get(items[which].toString()));
    else if (indexesOfSelectedStuff.contains(stuff.get(items[which].toString())))
        indexesOfSelectedStuff.remove(which);
    if (indexesOfSelectedStuff.size() == 0) {
        // disable button
        // but problem is, I don't know how to reference the POSITIVE_BUTTON
    }
    else {
        // enable button
    }
}

I know there's a high probability that I can't insert it there. And that I'd have to do the checks after the AlertDialog is created. But if that is the case, what would you recommend that I use? I'm thinking of Listeners but which? And how should I go about it?




Aucun commentaire:

Enregistrer un commentaire