I am trying to create a AlertDialog with multiple choice option. I have tried with the setMultiChoiceItems
but what i have is a ArrayList<Category>
and not a CharSequence
so i tried with the adapter.
The problem with setAdapter
is that when i select one item it closes the dialog window. And what i want is to select the items and then hit the OK button to see what items where selected.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
ArrayAdapter<Category> catsAdapter = new ArrayAdapter<Category>(this, android.R.layout.select_dialog_multichoice,this.categories);
builder.setAdapter(catsAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do something
}
});;
AlertDialog alert = builder.create();
alert.show();
see below code it may help you. i used this in my app.
alert_list.xml
make it right if it is correct.
Unfortunately, there doesn't seem to be an easy way to toggle on AlertDialog's multichoicemode without calling
setMultiChoiceItems()
.However, you can set an adapter, then turn on multichoice mode in the contained
ListView
itself.this will stop ur dialog from disappearing after one selection.
To get which items are selected , you need to plan your adapter accordingly.