Android “可展开列表视图”复选框正在选中多个复选框
我有一个带有子复选框的可扩展列表视图。该复选框在id为“check1”的子帮助器xml中定义。出于某种原因,多个复选框似乎链接在一起,而不是相互独立。例如,如果在第一个组下,我选择了第一个项目,它会选择该组中的每三个项目,其余所有组都会选择第二个项目,跳过两个项目并选择下一个项目 如果我选择组1中的前三个项目,则它将选择所有组中的所有项目。不知何故,我为每个孩子创建了3组复选框,而不是单独的复选框。我一直在看很多教程,这些教程都建议使用自定义适配器解决这个问题。我正在使用一个自定义适配器,但是,我不知道如何使用它的所有功能 也许更好、更短的问题是,我应该如何区分每个孩子复选框 谢谢 以下是我的侦听器的代码:Android “可展开列表视图”复选框正在选中多个复选框,android,expandablelistview,expandablelistadapter,android-checkbox,Android,Expandablelistview,Expandablelistadapter,Android Checkbox,我有一个带有子复选框的可扩展列表视图。该复选框在id为“check1”的子帮助器xml中定义。出于某种原因,多个复选框似乎链接在一起,而不是相互独立。例如,如果在第一个组下,我选择了第一个项目,它会选择该组中的每三个项目,其余所有组都会选择第二个项目,跳过两个项目并选择下一个项目 如果我选择组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
以解决此复选框问题 首先,创建一个
ArrayList<String> isCheckedStatus = new ArrayList<String>();
CheckBox check_document;
这将在每次调用getchildview方法时更改复选框状态。
并解决多复选框选择问题
希望这能奏效。它对我有用…很抱歉,我不太清楚您设置的标签,但我很肯定它与listview回收有关。它重复检查列表项,因为它正在回收这些视图。对于在getView()中膨胀的每个视图,请确保根据存储的数组选中/取消选中。标记是什么意思?我将尝试使用listview,我假设您正在谈论getChildView?
for (int i = 0; i < YOUR LIST ITEM ARRAY.size(); i++) {
isCheckedStatus.add("false");
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {if (isCheckedStatus.size() > 0) {
check_document = (CheckBox) convertView.findViewById(R.id.summary_chk);
if (isCheckedStatus.get(childPosition).equalsIgnoreCase("false")) {
((CheckBox) convertView.findViewById(R.id.summary_chk)).setChecked(false);
} else if (isCheckedStatus.get(childPosition).equalsIgnoreCase("true")) {
((CheckBox) convertView.findViewById(R.id.summary_chk)).setChecked(true);
}
check_document.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("AAAA size" + isCheckedStatus.size());
if (((CheckBox) v).isChecked()) {
if (isCheckedStatus.size() > 0) {
isCheckedStatus.remove(childPosition);
isCheckedStatus.add(childPosition, "true");
}
} else {
if (isCheckedStatus.size() > 0) {
isCheckedStatus.remove(childPosition);
isCheckedStatus.add(childPosition, "false");
}
}
}
});
}