1

I am using AlertDialog.Builder.setMultiChoiceItems to show checkboxes with texts. 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) {
        }
    });
soran_glekovec
  • 93
  • 2
  • 11
  • Maybe you can view this? https://stackoverflow.com/questions/10895763/checkbox-unchecked-when-i-scroll-listview-in-android – yong.k Aug 02 '17 at 05:22
  • @Rucha Bhatt So then I guess I would need to create a new custom dialog with listview? – soran_glekovec Aug 02 '17 at 05:44
  • 1
    It's a bug in the AppCompat library. Some versions have the bug and some do not. For example, 23.2.1 has the bug on Android 6 only, but 23.0.1 does not. You can see this list of available library versions here: https://maven.google.com – Mr-IDE Dec 29 '17 at 10:20
  • 1
    @soran_glekovec Did you ever get an answer to your question? I have run into the same issue and am looking for solutions. – Brian Jan 09 '18 at 17:42
  • @Brian No, I've decided to use different method for my project. – soran_glekovec Jan 09 '18 at 17:56
  • Ok ... thanks for the info. – Brian Jan 09 '18 at 17:57

2 Answers2

0

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

Amit Verma
  • 391
  • 2
  • 18
-1

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(); then on your setMultiChoiceItems do the following -

.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. Otherwise you might have to take care of the braces on your own :)

Kapil G
  • 4,081
  • 2
  • 20
  • 32
  • Thanks, but I can get the states of checkboxes to display them, problem occurs only when I scroll, that some of checkboxes become unchecked. – soran_glekovec Aug 02 '17 at 05:39
  • Ok So what is your list of selectedItems? Are you updating it in your onclicl – Kapil G Aug 02 '17 at 05:47
  • and what is selected list.@soran_glekovec I think you are interchanging 2 things. Either update the selectedItems list and pass that to setMultiChoiceItems method or pass selected list – Kapil G Aug 02 '17 at 05:50
  • It's not the the onClick listener that's problem, but keeping the state of checkboxes, which are already selected when dialog is created, when scrolling. – soran_glekovec Aug 02 '17 at 06:03
  • Yes man i know. Can you try the above code. when you call the set method, the second parameter is the list of items that should be checked. When you scroll it gets called again. So thats why you update the same parameter inside on click which is selectedItems – Kapil G Aug 02 '17 at 06:07
  • Tried above code but can't get it to work. selectedItems are boolean value, so I cant add an integer value to it, but use instead selected[which] = isChecked;, as for getListView().setItemChecked I've tried with ((AlertDialog) dialog).getListView().setItemChecked(which, isChecked);, but the result is still the same. – soran_glekovec Aug 02 '17 at 06:30
  • Ohk then you will have to handle it by creating a custom dialog with list. – Kapil G Aug 02 '17 at 06:51