Android: select an item in the multi select listview in the alertdialog
I am a novice in Android development and struggle with how to select some items in the list view hosted by alertdialog. In the following code, lv.setitemchecked does not work because listview has not been generated, so I want to know if there are any listview or alertdialog events to confirm that the view has been generated
String [] values = {"a","b","c"};
ArrayAdapter<String> adp = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, values);
ListView lv = new ListView(this);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(this);
lv.setAdapter(adp);
AlertDialog.Builder bldr = new AlertDialog.Builder(this);
bldr.setTitle("Select");
bldr.setView(lv);
bldr.setPositiveButton("Done",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handleDone();
}
});
bldr.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handleCancel();
}
});
final Dialog dlg = bldr.create();
dlg.show();
Never mind, I see. I call lv.setitemchecked (0, true) immediately after the call to lv.setadapter(). Once I move it after DLG. Show(), it's like a charm
resolvent:
public class DialogoSeleccion extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String[] items = {"Español", "Inglés", "Francés"};
AlertDialog.Builder builder =
new AlertDialog.Builder(getActivity());
builder.setTitle("Selección")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.i("Dialogos", "Opción elegida: " + items[item]);
}
});
return builder.create();
}
}
You'll get something like this:
If you want to remember or display the last selected item, just change the setitems method of set setsingchoiceitems() or setmultichiceitems(). It's easy to use setsinglechoiceitems(), just pass other parameters (set the selected index, and pass - 1 if you don't want to):
builder.setTitle("Selección")
.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Log.i("Dialogos", "Opción elegida: " + items[item]);
}
});
Using the code snippet above, you will have something similar
If you want a multichoose, you should change the method. The second parameter is not an integer, but a Boolean array. In this way, you will set ID whether any option is enabled:
builder.setTitle("Selección")
.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item, boolean isChecked) {
Log.i("Dialogos", "Opción elegida: " + items[item]);
}
});
The result will be:
The method to call any thre example is:
FragmentManager fragmentManager = getSupportFragmentManager();
DialogoSeleccion dialogo = new DialogoSeleccion();
dialogo.show(fragmentManager, "tagSeleccion");
If you know Spanish, this guide will help you: Complete Guide for alertdialogs or get a complete example at GitHub