以编程方式取消选中AlertDialog检查列表框

时间:2013-05-23 08:23:34

标签: android checkbox android-alertdialog

我需要以编程方式取消选中复选框,但检查列表在对话框中。这会改变解决方案吗?到目前为止我所拥有的:

CharSequence[] items = itemList.toArray(new CharSequence[itemList.size()]);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select categories");

builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  if (isChecked){
    if(chosenCat.size() < 10){
      chosenCat.add(items[which].toString());
    }else{
      Toast.makeText(getApplicationContext(), "Max 10 categories to compare.",   Toast.LENGTH_SHORT).show();
      //uncheck
      items.isChecked = false; // --> wrong
      chosenCat.remove(items[which].toString());
    }
  }else{
    chosenCat.remove(items[which].toString());
  }
}
});

2 个答案:

答案 0 :(得分:3)

我错过了这一行

((AlertDialog) dialog).getListView().setItemChecked(which, false);

checkboxStatusArray[1] = false;

答案 1 :(得分:0)

boolean[]传递到builder.setMultiChoiceItems,表示已分别添加的复选框的复选框状态(已选中/未选中)。请尝试此示例代码

boolean[] checkboxStatusArray = {true,false};

CharSequence[] items = itemList.toArray(new CharSequence[itemList.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select categories");

builder.setMultiChoiceItems(items, checkboxStatusArray,
new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int which, boolean isChecked) {


                      checkboxStatusArray[1] = true;//This line will enable second check box



     }
 });

builder.show();