user2365568 user2365568 - 3 months ago 33x
Android Question

Android setOnCheckedChangeListener calls again when old view comes back

I cannot solve an issue with the getGroupView-method.

the problem is that the listener setOnCheckedChangeListener is getting invoked to many times.

Let say i check a certain checkbox-item. Then I scroll it out of view and then scroll back. What happends is that the listener is called once again. And the problem is that I store checkbox-id's in an arraylist inside this listener to use it later in the code. The consequence is that more elements is added to the arraylist everytime the listener is called and distortes the data.

Is there a solution to this? what should I do? Should I for instance unregister the listener?

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View view = null;

final int group_position = groupPosition;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_phrase, parent, false);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(;
viewHolder.checkBox = (CheckBox) view.findViewById(;
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub

if (buttonView.isChecked()) {
checked.add((Integer) viewHolder.checkBox.getTag());
else {
checked.remove((Integer) viewHolder.checkBox.getTag());



} else {
view = convertView;

ViewHolder holder = (ViewHolder) view.getTag();

for (int i = 0; i < checked.size(); i++) {
if (checked.get(i) == group_position) {
else if (checked.get(i) != group_position) {

return view;


What version of Android are you using?

It seems to be an issue for multiple components, especially with a checkedChange() method (CheckBox, RadioButton) and I can't provide a good explanation why it is happening.

I would assume because it registers the change of the position state and grabs the change of other properties? A similar issue was addressed here

In any case, a workaround around this could be to add an OnClickListener().

CheckBox yourCheckBox = (CheckBox) findViewById (;

yourCheckBox.setOnClickListener(new OnClickListener() {

      public void onClick(View v) {
                //is chkIos checked?
        if (((CheckBox) v).isChecked()) {
                         //Case 1
          //case 2