Viren February 2016

Android: Expandable ListView with parent checkbox and child checkbox

I want to create a expandable listview with parent checkboxes and child checkboxes. Whenever a parent checkbox is selected all the child checkboxes should also be selected. Similarly whenever child checkboxes are selected/unselected the parent checkboxes should behave appropriately.

I have tried with below Adapter code after referring to this link Create expandable listview with group and child checkbox.

public class FilterListExpandableListItemAdapter extends BaseExpandableListAdapter {

private Map<ExpListGroupItem, List<ExpListChildItem>> groupChild;
private List<ExpListGroupItem> groups;
private Activity context;
private ExpandableListView elv;

public FilterListExpandableListItemAdapter(Activity context, List<ExpListGroupItem> cities,
                             Map<ExpListGroupItem, List<ExpListChildItem>> cityArea, ExpandableListView elv) {
    this.context = context;
    this.groupChild = cityArea;
    this.groups = cities;
    this.elv = elv;
}

@Override
public int getGroupCount() {
    return groups.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    return groupChild.get(groups.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return groups.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return groupChild.get(groups.get(groupPosition)).get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return true;
}

@Override
public View getGroupView(int groupPosition, boolean isE        

Answers


Ragesh Ramesh February 2016

In your getChildView method you should check if parentcheckbox is selected and if yes then

childHolder.childCheckBox.setSelected(true); 

Post Status

Asked in February 2016
Viewed 1,384 times
Voted 6
Answered 1 times

Search




Leave an answer