I’m working with an Expandable List, like this:

The Expandable list activity has a method called “OnGroupExpand” which allows you to perform some work before the parent expands and it’s children are exposed.
In my case, I want to attach a checkChangeHandler to each child’s checkbox so that when it’s clicked, I can mark the child as complete. I have the list showing up and the children show when the parent expands, but my check box does nothing.
Then, I try to add checkbox handlers dynamically in the “onGroupExpand” method. However, when onGroupExpand is triggered I get an illegal cast exception when trying to get a reference to each child view.
public void onGroupExpand (int groupPosition) {
int numOfChildren = expListAdapter.getChildrenCount(groupPosition);
for(int i = 0; i < numOfChildren; i++)
{
//Get exception here because getChild() returns Object - but how else can
//can I attach a check box to each child?
View v = (View) expListAdapter.getChild(groupPosition, i);
CheckBox cb = (CheckBox)v.findViewById( R.id.checkComplete );
cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
Toast.makeText(getBaseContext(), "Check Changed for " + groupPosition, 2000);
}
});
}
My primary question is about getting a reference to the Child view so that I can attach a handler dynamically. Please see the code comment where I’m trying to do this.
Thanks for your time…
I believe the problem is that the
Doesn’t return a
Viewbut is returning the data for that position. You might be using strings or a prefs object or something else. You will get your underlying object for it, you can make changes to that object. When the view is created in the method below (and this is called automatically by the ListView), you can setup any onCheckChanged listeners:Adding example code
Here is the example Adapter that does what you wanted to do. The Views are created on the Adapter and not the Activity. I assume you are creating a SimpleExpandableListAdapter in your Activity. Instead of that, add the following class and create that view.