我有一个带有子复选框的可扩展列表视图。该复选框在 child_helper xml 中定义,ID 为“check1”。由于某种原因,似乎多个复选框链接在一起,而不是每个复选框都是独立的。例如,如果在第一个组下我选择第一个项目,它会选择该组中的每个第三个项目,并且它选择第二个项目的所有其余组,跳过两个并选择下一个项目。
如果我选择第 1 组中的前三个项目,那么它将选择所有组中的所有项目。不知何故,我创建了 3 组复选框,而不是为每个孩子创建一个单独的复选框。我一直在看很多教程,这些教程表明问题已通过自定义适配器解决。我正在使用自定义适配器,但是,我不确定如何使用它的所有功能。
也许更好,更短的问题是我应该如何区分每个子复选框?
谢谢
这是我的听众的代码:
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d( "Enter_Foods", "onChildClick: "+childPosition );
CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
if( cb != null )
cb.toggle();
return false;
}
});
}
我认为问题出在我的适配器中被覆盖的方法之一。
ExpandableListAdapter foodCategoryExpand = new ExpandableListAdapter() {
@Override
Allitemsenabled()
other override methods...
public Object getChild(int groupPosition, int childPosition) {
switch (groupPosition) {
case 0:
return group1.get(childPosition);
case 1:
return group2.get(childPosition);
case 2:
return group3.get(childPosition);
default:
return null;
}
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
if (convertView == null) {
holder = new ChildHolder();
LayoutInflater inflator = LayoutInflater.from(Enter_Foods.this);
convertView = inflator.inflate(R.layout.enter_foods_child_helper,null);
holder.tvChild = (TextView) convertView.findViewById(R.id.enter_foods_exp_lay_child);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
//switch based on which group (main heading) child is in
switch (groupPosition) {
case 0:
holder.tvChild.setText(group1.get(childPosition));
break;
case 1:
holder.tvChild.setText(group2.get(childPosition));
break;
case 2:
holder.tvChild.setText(group3.get(childPosition));
break;
}
return convertView;
}
//create Group (main heading) view
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
if (convertView == null) {
holder = new GroupHolder();
LayoutInflater inflator = LayoutInflater.from(Enter_Foods.this);
//inflate xml file and then get view from that file
convertView = inflator.inflate(R.layout.enter_foods_group_helper,null);
holder.tvGroup = (TextView) convertView.findViewById(R.id.enter_foods_exp_lay_group);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
//this actually gets the text we set in onCreate and sets it to the textView (holder)
holder.tvGroup.setText(groupTitle.get(groupPosition));
return convertView;
}
other override methods