How uncheck items in AlertDialog (setMultiChoiceItems)?

前端 未结 4 1710
广开言路
广开言路 2020-12-06 16:49

I\'d like to clear selected items when the total came to three items selected, I am doing as follows but is not working ...

AlertDialog.Builder builder = new         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 17:25

    if you want to use multicheckoption as single selection option then use this code.

     String[] items = new String[]{"Most Funded (high - low)", "Most Funded (low - high)", "Newest first", "Funding Ask"};
    boolean selected[] = new boolean[]{false, false, false, true};
    
    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getText(R.string.sortby));
        builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {
    
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                //
                for (int i = 0; i < selected.length; i++) {
                    if (i == which) {
                        selected[i]=true;
                        ((AlertDialog) dialog).getListView().setItemChecked(i, true);
                    }
                    else {
                        selected[i]=false;
                        ((AlertDialog) dialog).getListView().setItemChecked(i, false);
                    }
                }
            }
    
        });
        builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }
    

提交回复
热议问题