I need to show a list of checkbox list items as CHECKED in ALERTDIALOG by default(when the alertbox pops up) in android.
This is my code,
final String[] items = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
final List<String> itemlist = Arrays.asList(items);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Working days")
.setMultiChoiceItems(items, seletedvaluesbool, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
} else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the array, remove it
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on Cancel
}
}).create();
dialog.show();
}
When the dialog pops up, I need to show few items as Checked by default. I tried below code for that but it's not working.
ListView lv = ((AlertDialog) dialog).getListView();
dialog.getListView().setSelection(0); //To set the 0th position as checked
Any help would be great. Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire