简体   繁体   English

滚动时,对话框中的复选框变为未选中状态

[英]Checkboxes become unchecked in Dialog when scrolling

I am using AlertDialog.Builder.setMultiChoiceItems to show checkboxes with texts. 我正在使用AlertDialog.Builder.setMultiChoiceItems显示带有文本的复选框。 I can display the checked items successfully, but whenever I scroll it down or up, some of them become randomly unchecked. 我可以成功显示选中的项目,但是每当我上下滚动它时,其中的一些就会被随机取消选中。 Below is my code. 下面是我的代码。

What can I do to fix this? 我该怎么做才能解决此问题? Any help appreciated! 任何帮助表示赞赏!

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Title")
    .setMultiChoiceItems(items, selectedItems,
            new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which,
                                    boolean isChecked) {
                    selected[which] = isChecked;
                }
            })
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    })
    .setNegativeButton(R.string.preklici, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {
        }
    });

I think you are implementing checkbox in list View item so you can go through this answer. 我认为您正在实现列表视图项中的复选框,以便可以通过此答案。 https://stackoverflow.com/a/10896140/6869491 hope it will help https://stackoverflow.com/a/10896140/6869491希望它会有所帮助

You need to handle the check state of the checkbox in the code. 您需要处理代码中复选框的检查状态。 Create a list of already selected items 创建已选择项目的列表

So create ArrayList<Integer> selList=new ArrayList(); 因此,创建ArrayList<Integer> selList=new ArrayList(); then on your setMultiChoiceItems do the following - 然后在您的setMultiChoiceItems执行以下操作-

.setMultiChoiceItems(items, selectedItems,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {

                            // If user select a item then add it in selected items
                            selectedItems.add(which);
                            multichoiceDialog.getListView().setItemChecked(which, isChecked);// You can tell the dialog to update its state here. 

                    }
                }

Please note I have only shown 1 part of your code so only use the content inside the setMultiChoiceItems method. 请注意,我只显示了代码的一部分,因此仅使用setMultiChoiceItems方法中的内容。 Otherwise you might have to take care of the braces on your own :) 否则,您可能需要自己照顾大括号:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM