开发者

How uncheck items in AlertDialog (setMultiChoiceItems)?

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 AlertDialog.Builder(this);
builder.setTitle(getResources().getText(R.string.escolhaArquivosBaixados));
builder.setMultiChoiceItems(items, selected, new DialogInterface.OnMultiChoiceClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        //                  
        int count = 0;
        for(int i = 1; i < selected.length; i++){
            //
            if (select开发者_JAVA技巧ed[i]){
                count++;
            }
            if (count == 3){
                //enter here but nothing happens
                ((AlertDialog) dialog).getListView().setItemChecked(which, false);
                break;
            }
        }
    }
});


Seeing Jorgesys answer in this question I realized what was missing in my code, is necessary to change the boolean list too.

        selected[which] = false;
        ((AlertDialog) dialog).getListView().setItemChecked(which, false);


The first index in an array is 0, not 1. So this:

for(int i = 1; i < selected.length; i++){
                //
                if (selected[i]){
                    count++;
                }

Is never going to check the first item in the boolean array. You need to start with i == 0. I don't know how many items are in your list. But if you only have 3 items then

if (count == 3){

won't ever be true because its only going to check the last two in the array. Also this call:

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

is only going to set 1 item in the list to unchecked. It will be the 3rd one that you click. So the first two that you click are going to get checked and stay checked. Then when you click on the third one it will get checked for a split second and then uncheck itself. Is that what you are trying to do? or do you want to uncheck all 3 of them? Its not very clear which you are trying to do by your question.


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();
}


for (int i = 0; i < visitArray.length; ++i) {
                    _selections[i] = false;
                    ((AlertDialog) dialog).getListView().setItemChecked(i, false);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜