AlertDialog MultiChoiceItems Listener problems

☆樱花仙子☆ 提交于 2019-12-04 04:08:20

OH!!! I forget it to ensure deselect you must change checked to null ;), I had the same issue.

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

To deselect the other items it works fine::

if(item == ANY_ITEM_BUT_0){    
   for(int i=0; i<items.length;i++){  
            if (item != ANY_ITEM_BUT_0)                             
           ((AlertDialog)dialog).getListView().setItemChecked(i, false);
    }
}

Dont think you can change the values in the list since the list-items (checkboxes) are controlled by the builder-object. However, you could simply make the dialog re-initiate when the first item is clicked... by dismissing the dialog that is showing, and create a new one....

Bilodeau

If you want to set a check box to not be checked and you need to set the checkedItems array on the call to setMultiChoiceItems(), you need to set the checked array items to false as well. Make sure your checked array is final so you can access it in the listener.

builder.setMultiChoiceItems(list, checked,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
 int item, boolean isChecked) {

if(isChecked && item == ANY_ITEM_BUT_0)
{
   for(int i=0; i<list.length;i++){  
      if (i != ANY_ITEM_BUT_0) {   
       checked[i] = false;                          
       ((AlertDialog)dialog).getListView().setItemChecked(i, false);
        } 
     }
   }
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!