简体   繁体   中英

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 isExpanded, View convertView, ViewGroup parent) {
   ExpListGroupItem group = (ExpListGroupItem) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.filter_drawer_list_parent_item,
                null);
    }
    GroupViewHolder groupHolder = new GroupViewHolder();
    groupHolder.parentCheckBox = (CheckBox) convertView.findViewById(R.id.chkbox_filter_drawer_city);
    groupHolder.parentCheckBox.setTypeface(null, Typeface.BOLD);
    groupHolder.parentCheckBox.setText(group.getGroupName());
    groupHolder.parentCheckBox.setSelected(group.isSelected());
    GroupCheckChangedListener groupCheckChangedListener = new GroupCheckChangedListener();
    groupHolder.parentCheckBox.setOnCheckedChangeListener(groupCheckChangedListener);
    convertView.setTag(groupHolder);
    return convertView;
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent){
    final ExpListChildItem child = (ExpListChildItem) getChild(groupPosition, childPosition);
    LayoutInflater inflater = context.getLayoutInflater();

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.filter_drawer_list_child_item, null);
    }
    ChildViewHolder childHolder = new ChildViewHolder();
    childHolder.childCheckBox =(CheckBox) convertView.findViewById(R.id.chkbox_filter_drawer_area);
    childHolder.childCheckBox.setSelected(child.isSelected());
    childHolder.childCheckBox.setText(child.getChildName());
    convertView.setTag(childHolder);
    return convertView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return true;
}

static class ChildViewHolder {
   CheckBox childCheckBox;
}

static class GroupViewHolder {
    CheckBox parentCheckBox;
}


private class GroupCheckChangedListener implements CheckBox.OnCheckedChangeListener{

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        ExpListGroupItem group = new ExpListGroupItem();
        CheckBox chk = (CheckBox) compoundButton;
        group.setGroupName(chk.getText().toString());
        for(ExpListChildItem item: groupChild.get(group)){
            item.setIsSelected(true);
        }
        notifyDataSetChanged();

    }
}}

The problem here is I have checked with the debugger option . My underlying data has changed. But is not reflecting on the UI . Even when I check the parent, corresponding children are not selected.

Can someone please suggest what is it that I am doing wrong here? Is this the best way to handle the scenario required?

在您的getChildView方法中,您应该检查是否选择了parentcheckbox,如果是,则

childHolder.childCheckBox.setSelected(true); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM