I have a listView in my app. For each element in the listView, it has a Switch. Previously I implemented an onClickListener for the Switch inside the listView Adapter's getView method. However, I found that although it does work when user clicks on the switch, it does not work when user slide on the Switch. Then I try to change the onClickListener to onCheckedChangeListener. It works when user click or slide on the Switch, however, when I scroll the the listView so that the element disappear, I found that the Switch which is originally checked become unchecked.
Could you help? Below is my code for the getView method in the listView Adapter:
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
class viewHolder {
public TextView tvSceneName;
public Switch swSelectSwitch;
}
final viewHolder holder;
final View v = convertView;
// Use ViewHolder to avoid findViewById each time the user scroll
if (convertView == null) {
holder = new viewHolder();
convertView = mInflater.inflate(R.layout.scene_list_element, parent, false);
holder.tvSceneName = (TextView) convertView.findViewById(R.id.tvSceneName);
holder.swSelectSwitch = (Switch) convertView.findViewById(R.id.swScene);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
if(holder.swSelectSwitch != null) {
// Set the switch to correct on/off status
holder.swSelectSwitch.setChecked(mDataSource.get(position).get_isOn());
// Set up On checked change listener for the switch
holder.swSelectSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
// Set the new status to arraylist
Switch sw = (Switch) v.findViewById(R.id.swScene);
MainActivity.sceneArrayList.get(position).set_isOn(sw.isChecked());
});
}
}
EDIT: On further test, I found there is bug in my newly added onCheckChangedListener (because I copied some code from the onClickListener). Also I have found a solution, it is to set the onCheckChangedListener to null before I set the Switch by code.
if(holder.swSelectSwitch != null) {
// Set the switch to correct on/off status
holder.swSelectSwitch.setOnCheckedChangeListener(null);
holder.swSelectSwitch.setChecked(mDataSource.get(position).get_isOn());
// Set up On checked change listener for the switch
holder.swSelectSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
// Set the new status to arraylist
mDataSource.get(position).set_isOn(isChecked);
});
}