我正在使用expandablelistview,复选框作为标题和子数据。当用户展开组并检查组标题时,我也想检查所有子复选框。我尝试了一些解决方案但从未获得过。 请提前帮助我。
**my code is like this**
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listChildData;
private HashMap<Integer, boolean[]> mChildCheckStates;
private ChildViewHolder childViewHolder;
private GroupViewHolder groupViewHolder;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listChildData = listChildData;
mChildCheckStates = new HashMap<Integer, boolean[]>();
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listChildData.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
final String childText = (String) getChild(mGroupPosition, mChildPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.childitem, null);
childViewHolder = new ChildViewHolder();
childViewHolder.mChildText = (TextView) convertView.findViewById(R.id.childTextView);
childViewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.childcheck);
convertView.setTag(R.layout.childitem, childViewHolder);
}else {
childViewHolder = (ChildViewHolder) convertView.getTag(R.layout.childitem);
}
childViewHolder.mChildText.setText(childText);
childViewHolder.mCheckBox.setOnCheckedChangeListener(null);
if (mChildCheckStates.containsKey(mGroupPosition)) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
childViewHolder.mCheckBox.setChecked(getChecked[mChildPosition]);
} else {
boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
mChildCheckStates.put(mGroupPosition, getChecked);
childViewHolder.mCheckBox.setChecked(false);
}
childViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
} else {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
}
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listChildData.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView( final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.groupitem, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.mGroupText = (TextView) convertView.findViewById(R.id.txtview1);
convertView.setTag(groupViewHolder);
}
else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.mGroupText.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public final class GroupViewHolder {
TextView mGroupText;
CheckBox mGroupCheckbox;
}
public final class ChildViewHolder {
TextView mChildText;
CheckBox mCheckBox;
}
public class ListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public ListAdapter(Context context, String[] values) {
super(context, R.layout.list, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.listdata);
textView.setText(values[position]);
return rowView;
}
}
}