0

I implemented multiple switches in CustomAdapter.

CustomAdapter code below.

    sche_swt = (Switch)convertView.findViewById(R.id.ctschedule);
    loc_swt = (Switch)convertView.findViewById(R.id.ctlocation);

    sche_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) {
            if (compoundButton.getId() == R.id.ctschedule) {
                if (is_checked == true) {
                    contactItemList.get(position).setSchedule(true);
                } else {
                    contactItemList.get(position).setSchedule(false);
                }
            }
        }
    });

    if (contactItemList.get(position).getScheduleInt() == 1) {
        sche_swt.setChecked(true);
    }
    else
        sche_swt.setChecked(false);

    loc_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) {
            if (compoundButton.getId() == R.id.ctlocation) {
                if (is_checked == true) {
                    contactItemList.get(position).setLocation(true);
                }
                else {
                    contactItemList.get(position).setLocation(false);
                }
            }
        }
    });

    if (contactItemList.get(position).getLocationInt() == 1) {
        loc_swt.setChecked(true);
    }
    else
        loc_swt.setChecked(false);


    return convertView;
}

One single line looks like

name
phone_number
switch 1 | switch 2

And I stored state of switches

For example :

1 line = switch(false) | switch(true)
2 line = switch(false) | switch(true)
3 line = switch(true)  | switch(false)

... and so on.

After I finished my app, re-executed listview result looks like

1 line = switch(true) | switch(false)
2 line = switch(true) | switch(false)
3 line = switch(true) | switch(false)

What's wrong??

Na Jun Yeop
  • 55
  • 1
  • 7

1 Answers1

0

Problem might be with using position variable in the listener or with calling the setChecked method. Try following, I only did for schedule switch, do the same for location switch.

sche_swt = (Switch)convertView.findViewById(R.id.ctschedule);
loc_swt = (Switch)convertView.findViewById(R.id.ctlocation);

// setting tag
sche_swt.setTag(position)
loc_swt.setTag(position)

// you need to remove the listener because when you call setChecked() 
// it will invoke the listener unnecessarily and it might affect the 
// state of the view.
sche_swt.setOnCheckedChangeListener(null);
if (contactItemList.get(position).getScheduleInt() == 1) {
    sche_swt.setChecked(true);
}
else
    sche_swt.setChecked(false);

sche_swt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean is_checked) {
        if (compoundButton.getId() == R.id.ctschedule) {
            // using the tag to get the position.
            int position = compoundButton.getTag();
            if (is_checked == true) {
                contactItemList.get(position).setSchedule(true);
            } else {
                contactItemList.get(position).setSchedule(false);
            }
        }
    }
});


return convertView;

If you don't know what tag is or why I used it and not position, read this https://stackoverflow.com/a/5291891/1749223 .I hope the above fixes the problem.

Binary Baba
  • 1,953
  • 2
  • 16
  • 23