问题
How do I access the child views of child items in any group view in expandable listview??
selectAll.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int gourpsSum = adapter.getGroupCount();
for(int i = 0; i < gourpsSum; i++) {
int childSum = adapter.getChildrenCount(i);
for(int k = 0; k < childSum;k++) {
boolean isLast = false;
if (k == (childSum - 1)){
isLast = true;
}
CheckBox cBox = (CheckBox) adapter.getChildView(i, k, isLast, null, null).findViewById(R.id.checkBox);
cBox.setChecked(selectAll.isChecked());
((BaseExpandableListAdapter) adapter).notifyDataSetChanged();
}
}
}
});
where selectAll is another Checkbox above the expandable listview.
回答1:
You need to make a class to extend the BaseExpandableListAdapter, and then override the abstract getChildView() method, this method is based on groupPosition and childPosition to determine which view to be displayed. You can also visit this Expandable List View tutorial.
In the tutorial, it shows how to access the TextView object in each child view, if you want to access the CheckBox, you can do the similar thing. Below is a sample code:
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
CheckBox checkboxListChild = (CheckBox) convertView
.findViewById(R.id.checkboxListItem); //assume thats the id of your checkbox object
txtListChild.setText(childText);
return convertView;
}
You can also check out this tutorial to learn about ExpandableListView with checkbox.
来源:https://stackoverflow.com/questions/26914488/access-child-items-individual-ojects-in-expandable-listview