bliny
bliny

Reputation: 67

Checkbox dialog not working

When I write this code, it only shows dialog with one button, but not showing check boxes.

I don't know what's the problem.

private void showDialog(){
        final ArrayList selectedItems = new ArrayList(); 
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.setTitle("asdasd");
        dialogBuilder.setMessage("asdasd");
        final String[] options = {"asd", "dsa","asd","aa"};



        dialogBuilder.setMultiChoiceItems(options, null, new DialogInterface.OnMultiChoiceClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                if(isChecked){
                    selectedItems.add(which);
                }else if(selectedItems.contains(which)){
                    selectedItems.remove(Integer.valueOf(which));
                }

            }
        });


        dialogBuilder.setNegativeButton("CANCEL",null);

        AlertDialog dialog = dialogBuilder.create();
        dialog.show();

        };

Upvotes: 0

Views: 60

Answers (1)

A23149577
A23149577

Reputation: 2155

Just see this link, and here is some code:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(R.string.pick_color);
           .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    return builder.create();
}

Hope you can solve your problem.

Upvotes: 1

Related Questions