我想创建一个expandable listview
parent checkboxes
和child checkboxes
。每当选择parent checkbox
时,也应选择所有child checkboxes
。同样,只要选择/取消选择child checkboxes
,parent checkboxes
应该表现得恰当。
在参考此链接后,我尝试使用以下Adapter
代码
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();
}
}}
这里的问题是我已经使用debugger option
进行了检查。
我的基础数据已经改变。但是没有反映在UI
上。
即使我检查父母,也没有选择相应的孩子。
有人可以建议我在这里做错了什么吗? 这是处理所需方案的最佳方法吗?
答案 0 :(得分:0)
在你的getChildView方法中,你应该检查是否选中了parentcheckbox,如果是,那么
childHolder.childCheckBox.setSelected(true);