如何解决更改 Multichoice AlertDialog
我一直在尝试实现一个多选警报对话框,在大多数情况下,一切都清晰易懂,但警报对话框从布尔数组中获取项目的状态,并且所有项目都设置为 true。如果在警报对话框中选中它,我无法弄清楚如何更改数组中项目的状态。
private void showCategorySelectionDialog() {
// Prepare the dialog by setting up a Builder.
final String selectionTitle = "Show on map: ";
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(selectionTitle);
final String[] categories = new String[]{"Camping grounds","Abandoned places","Nature areas","Lookout Points"};
// Find the current map type to pre-check the item representing the current state.
boolean[] checkedItems = new boolean[]{
true,true,true
};
// Add an OnClickListener to the dialog,so that the selection will be handled.
builder.setMultiChoiceItems(
categories,checkedItems,new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,int which,boolean isChecked) {
//check which item is clicked and if it was true then set it as false.
if (isChecked && checkedItems[which] == true){
checkedItems[which]= false;
}else{
//If item was clicked and the value was false then set it as true.
checkedItems[which] = true;
}
}
}
);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
// Build the dialog and show it.
AlertDialog categoryDialog = builder.create();
categoryDialog.setCanceledOnTouchOutside(true);
categoryDialog.show();
}
这个当前的解决方案不会改变值,我的假设是我以不正确的方式处理数组,但我不确定正确的方式会如何。
解决方法
由于您想在检查数组时更改元素的值,因此您可以尝试将该值设置为与通过 cannot read property paginationoption of undefined
方法传递的 isChecked
变量相等。
检查下面的代码:
onClick()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。