我需要在Android中默认情况下(在弹出警报框时)在ALERTDIALOG中显示复选框列表项的列表。
这是我的代码,
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
seletedvaluesbool[indexSelected] = isChecked;
} else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the array, remove it
seletedItems.remove(Integer.valueOf(indexSelected));
seletedvaluesbool[indexSelected] = isChecked;
}
}
}).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();
}
当对话框弹出时,默认情况下,我需要显示几项为“已选中”。我为此尝试了以下代码,但无法正常工作。
ListView lv = ((AlertDialog) dialog).getListView();
dialog.getListView().setSelection(0); //To set the 0th position as checked
任何帮助都会很棒。预先感谢。
答案 0 :(得分:0)
代替:
dialog.getListView().setSelection(0);
您应该使用此:
dialog.getListView().setItemChecked(0, true);