I'm displaying entries from SQLite database in Expandable ListView and everything is working correctly but, I wanted to add in a checkbox right next to all children. Every time I check a checkbox and then scroll down or contract the checkbox which I had checked it doesn't save the state of the checkbox. I know that the list is recycled whenever I scroll down and expand the groups and so forth. How can I save the state of every checkbox (by every child)? I have a class which extends CursorTreeAdapter and there is no "childPosition" in the methods. What do I need to do in order to save the state?
How to save state of checkboxes in expandable listview - Android
1.6k Views Asked by user990230 At
2
There are 2 best solutions below
0

One more solution is you can hold a counter for each element in list view. For example if you have Person list, you can add one counter variable in your Person class.
And in getChildView method, put all codes in an if block like below;
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
//FOR SAVE THE STATES!!
if(parents.get(groupPosition).getCounter()==0) {
DatePicker datePicker = null;
Button ok = null;
Spinner installment = null;
CheckBox creditCard;
......//BLA BLA BLA
//After finish your codes, increment the counter
parents.get(groupPosition).setCounter((parents.get(groupPosition).getCounter()+1));
}
return convertView;
}
Because for each expandGroup method called, all the initializations (for now those are in if block) repeats. Because of that you loose your state of objects.
Hope this help.
you can have an array of boolean flags with same size as sum of list elements and set and get the checked value from this array for any component.