onCheckedChanged fired multiple times, Listview with checkbox

Replace the onCheckChangeListener to onClickListener.

The checkChanged will be called twice as it will be called when you call setChecked() method and when you click on the checkbox.


I have fixed this issue just by checking:

    mSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
        if(buttonView.isPressed()){
            // do you operation
        });
        }
    });

It avoids multiple calling


That's expected behavior:

  • onCheckedChanged(CompoundButton buttonView, boolean isChecked) is called for every item, whenever they're checked/unchecked. Android has decided to track all items status for you and calls you for each item every time it was changed. With the isChecked parameter you're able to differentiate what happened.

  • onItemClick() is called whenever one of the items where clicked - that is not necessarily the checkbox within the item, but somewhere. Usually the item is selected afterwards - again, not always.

  • If you need to know which item was actually selected from the list view, use OnItemSelectedListener.onItemSelected(). This is the one called to get the selection (whole item).

BTW: You dont need to prgram the behavior of a checkbox manually. The check/uncheck and drawing of the tick in the box is done by Android. You just need to get the checked status once you know which one was selected. So the onCheckedChanged implementation is not necessary at all as far as I can see.