Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android checkbox dialog (easy)

I have a dialog with checkboxes, and I was trying to do different things when options are selected and when OK is pressed.

I thought I knew what I was doing after reading some tutorials, but when I press OK it just toasts "Everything" even if it isn't checked. So it seems that my if statements are not working correctly, but I don't know why.

What am I doing wrong and how do I fix it?

    final CharSequence[] items = {"Item 1", "Item 2", "Item 3"};
    final boolean[] states = {false, false, false};
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("What would you like to do?");

    builder.setMultiChoiceItems(items, states, new DialogInterface.OnMultiChoiceClickListener(){
        public void onClick(DialogInterface dialogInterface, int item, boolean state) {
        }
    });

    builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            SparseBooleanArray CheCked = ((AlertDialog)dialog).getListView().getCheckedItemPositions();
            if(CheCked.get(CheCked.keyAt(0)) == true){
                Toast.makeText(Backup.this, "Item 1", Toast.LENGTH_SHORT).show();
            }
            if(CheCked.get(CheCked.keyAt(1)) == true){
                Toast.makeText(Backup.this, "Item 2", Toast.LENGTH_SHORT).show();
            }
            if(CheCked.get(CheCked.keyAt(2)) == true){
                Toast.makeText(Backup.this, "Item 3", Toast.LENGTH_SHORT).show();
            }
        }
    });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
             dialog.cancel();
        }
    });

    builder.create().show();
}
like image 552
GFlam Avatar asked May 12 '11 19:05

GFlam


1 Answers

I would do it like this for example:

public class Main extends Activity {

    CharSequence[] items = {"Google", "Apple", "Microsoft"};
    boolean[] itemsChecked = new boolean[items.length];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button btn = (Button) findViewById(R.id.btnDialog);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(0);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case 0:
            return new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle("Dialog with simple text")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    for (int i = 0; i < items.length; i++) {
                    if (itemsChecked[i]) {
                        Toast.makeText(getBaseContext(), items[i] + " checked!", Toast.LENGTH_LONG).show();
                    }
                }
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_LONG).show();
                }
            })
            .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    Toast.makeText(getBaseContext(), items[which] + (isChecked ? "checked!" : "unchecked!"), Toast.LENGTH_SHORT).show();
                }
            })
            .create();
        }

        return null;
    }


}
like image 93
evilone Avatar answered Sep 28 '22 03:09

evilone