Java 选中ExpandableListView(Android)中的所有复选框
我正在使用Java 选中ExpandableListView(Android)中的所有复选框,java,android,Java,Android,我正在使用ExpandableListView。在组和子组中都有复选框。如何满足以下要求 当用户选中组的复选框时,选中其子项的所有复选框 我想我需要覆盖BaseExpandableListAdapter, 但我不知道该填什么。多谢各位 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { CheckBox cb = (CheckBo
ExpandableListView
。在组和子组中都有复选框。如何满足以下要求
当用户选中组的复选框时,选中其子项的所有复选框
我想我需要覆盖BaseExpandableListAdapter
,
但我不知道该填什么。多谢各位
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
CheckBox cb = (CheckBox)v.findViewById(R.id.checkBox1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
//Fill in what?
}
});
}
有一个类似的问题,但我不理解它的解决方案:
我相信链接的答案是,您应该在数组列表中跟踪已选中组的状态。然后,在
BaseExpandableListAdapter
的getChildView
方法中,检查是否存在子级的组
这是我的实现,我使用的是hashmap而不是arraylist:
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
CheckBox check__bookmark = (CheckBox) child_row.findViewById(R.id._checkBox);
if (mapp.group_bookmark_s_checked.containsKey((groupPosition)) == true)
check__bookmark.setChecked(true);
else
check__bookmark.setChecked(false);
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
CheckBox PlayGroupSelected =
(CheckBox) group_row.findViewById(R.id.bookmark_group_checkBox);
if (mapp.group_bookmark_s_checked.containsKey((groupPosition)) == true)
PlayGroupSelected.setChecked(true);
else
PlayGroupSelected.setChecked(false);
PlayGroupSelected.setOnCheckedChangeListener(
new BookmarkGroupCheckedChangeListener(groupPosition, mapp, group_row));
}
现在,在侦听器中,您可以通过删除和添加状态对象来管理状态。我使用了notifyDataSetChanged()
,因为在检查状态更改上绘制后,子项不会刷新。不确定这是否是解决问题的正确方法,但它确实起到了作用
class BookmarkGroupCheckedChangeListener implements OnCheckedChangeListener {
private int mPosition;
MyApplication mapp;
RelativeLayout mgroup_row;
BookmarkGroupCheckedChangeListener(int position, MyApplication app, RelativeLayout group_row) {
mapp = app;
mPosition = position;
mgroup_row = group_row;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
mapp.group_bookmark_s_checked.put((mPosition), new Boolean(true));
Log.v("DEBUG:", "Group Bookmark Checked On at " + mPosition);
notifyDataSetChanged();
} else if (mapp.group_bookmark_s_checked.containsKey(mPosition)) {
mapp.group_bookmark_s_checked.remove((mPosition));
Log.v("DEBUG:", "Checked Off at " + mPosition);
notifyDataSetChanged();
}
}
}
这里有一个链接:
里面有你需要的一切。只需通读代码并尝试一下。是的。notifyDataSetChanged();这很重要。坦克