3
我有一个带有子复选框的可扩展列表视图。复选框在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
我很抱歉,我不能完全弄清楚的东西标签已成立,但我很肯定的它是与ListView的回收利用。它重复检查列表项是因为它正在回收这些视图。对于你在getView()中膨胀的每个视图,确保你基于你存储的一些数组来检查/取消选中。 – Kgrover 2013-04-08 04:06:19
你是什么意思标签的东西?我会尝试搞乱listview,我假设你在谈论getChildView? – farnett 2013-04-08 05:48:44