How to save state of checkboxes in expandable listview.
I am using Expandable ListView for showing data in parent-child form, parent list view is working fine but in child view its changing its state of check box randomly on scroll.
Here is My code, these are the method of BaseExpandableListAdapter
This is childView method creating problem when scrolling and selecting multiple check box randomly.
private String[] groups = {
"Gluten",
"Wheat",
"Peanuts"
};
private String[][] children = {
{"item1", "item2","item3","item4","item5","item6","item7"},
{"item1", "item2","item3","item4","item5","item6","item7"},
{"item1", "item2","item3","item4","item5","item6","item7"}
};
@Override
public int getChildTypeCount() {
return children.length;
}
@Override
public int getGroupTypeCount() {
return groups.length;
}
@Override
public View getChildView(int i,final int i1,final boolean b, View view,
ViewGroup viewGroup) {
if(view == null)
{
view = LayoutInflater.from(ProfileActivity.this).inflate(R.layout.list_item, null);
childHolder = new ChildHolder();
childHolder.cCheckBox = (CheckBox) view.findViewById(R.id.checkbox2);
childHolder.cTextView = (TextView) view.findViewById(R.id.lblListItem);
view.setTag(childHolder);
Log.e(TAG, " if" + b+"");
}
else
{
Log.e(TAG, "else" + b+"");
childHolder = (ChildHolder) view.getTag();
}
Log.e("TAG", "getchildView:" + b + " :: state :" + childHolder.state + " :: childHolder :" + childHolder.toString());
childHolder.cTextView.setText(getChild(i, i1).toString());
childHolder.cCheckBox.setChecked(childHolder.state);
childHolder.cCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.e("child Check Box", childHolder.cTextView.getText() + "==========" + isChecked);
childHolder.state = isChecked;
}
});
return view;
}
These are view holder internal classes
class GroupHolder {
CheckBox checkBox;
TextView title;
ChildHolder childHolder;
}
class ChildHolder{
CheckBox cCheckBox;
TextView cTextView;
Boolean state = false;
}
as in image i checked only one child item but randomly it selects other child item also