0

Ok guys...I need to create a Alert dialog with 3 check boxes. If the top check box is clicked, 2 another one should be clicked and disabled !! I do them clicked, but not disabled. And i have no idea how to do that.

@Override 
protected Dialog onCreateDialog (int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);        
builder.setTitle("AA");             
builder.setMultiChoiceItems(mStrings, mCheckedItems, new DialogInterface.OnMultiChoiceClickListener() {                     
public void onClick(final DialogInterface dialog, int which, boolean isChecked) {


                        switch (which) {
                        case 0: {

                        if(isChecked==true)  {
                            for (int i = 1; i<=2; i++) {                                    
                            ((AlertDialog) dialog).getListView().setItemChecked(i, true);                           
                             }
                        }

                        if (isChecked==false) {
                            for (int i = 1; i<=2; i++) {                                    
                                ((AlertDialog) dialog).getListView().setItemChecked(i, false);                          
                                 }

                             break;
                        }

And this solution is not good to. Some times its not click all checkboxes. Have anybody any idea ?

Jim
  • 8,874
  • 16
  • 68
  • 125

3 Answers3

3

You should be able to call .setEnabled(false) on the two checkboxes you want to disable in your onClick() listener. Out of curiosity why are you using a for loop structure to loop thru 2 items and set them to checked. It seems to me that calling .setChecked() on both of the in 2 successive calls would simplify this proccess.

code sample:

//This line has to go after your dialog.show(); call
    CheckBox chkBox = (CheckBox) dialog.findViewById(R.id.yourCheckBox);
//This line will go in your OnClickListener.
    chkBox.setEnabled(false);
FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Then tell me...How to setEnabled(false) 2 other check boxes in my cod ? Can you wtite a cod please ? – Jim Feb 04 '11 at 08:13
1
/* Please set appropriate boolean value in the boolean array which you have
passed as paramater for
 builder.setMultiChoiceItems(StringArray,BooleanArray, Listener)
in order to check or uncheck items in dialog */

@Override 
protected Dialog onCreateDialog (int id) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("AA"); 

builder.setMultiChoiceItems(mStrings, mCheckedItems,
        DialogInterface.OnMultiChoiceClickListener() { 
    public void onClick(final DialogInterface dialog, int which, boolean isChecked) { 

        switch (which) {
            case 0: {
                if(isChecked)  {
                    for (int i = 1; i<=2; i++) {
                        mCheckedItems[i] =false;
                    }
                } else {
                    for (int i = 1; i<=2; i++) {
                        ((AlertDialog) dialog).getListView().setItemChecked(i,false);
                        mCheckedItems[i] =false;
                    }
                }
                break;
        }
nrallakis
  • 161
  • 11
Sumith.Pattar
  • 100
  • 1
  • 8
0

make mCheckeditems[i]=false if u want the checkbox unchecked or vice versa

Girish
  • 2,196
  • 2
  • 18
  • 24