Friday, June 3, 2011

Silence O'Clock 2.1

Just a quick post to note that I've updated Silence O'Clock to fix a pretty weird bug. Sorry for any inconvenience!

If anyone else stumbles on a weird issue where they've got checkboxes attached to views in an ExpandableListView, and expanding/hiding the groups causes the checkboxes to go crazy, you might be doing something like this in your getChildView or getGroupView:

final Item item = getChild(groupPosition, childPosition);
CheckBox enabled = (CheckBox) convertView.findViewById(R.id.enabled);
enabled.setChecked(item.enabled);
// reset with a shiny new listener
enabled.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do something with "item"
}
});


The problem is that hiding/showing the views actually causes them to be recreated. But, in an effort to be memory friendly, Android just reuses old Views. So, your call to setChecked() is actually firing the old OnCheckedChangeListener that was attached earlier, and which has reference to a different Item.

The easy solution is to just clear the listener with enabled.setOnCheckedChangeListener(null); before you call setChecked(). Easy!