检查以下示例可扩展列表适配器
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
private ArrayList<ArrayList<Boolean>> checkboxStatus = new ArrayList<ArrayList<Boolean>>();
public MyExpandableListAdapter() {
int groupCount = groups.length;
for(int i=0; i<groupCount; i++)
{
int childCount = children[i].length;
ArrayList<Boolean> childStatus = new ArrayList<Boolean>();
for(int j=0; j<childCount; j++) {
childStatus.add(false);
}
checkboxStatus.add(childStatus);
}
}
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ExpandableListCheckboxActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
CheckBox chkbox = new CheckBox(ExpandableListCheckboxActivity.this);
chkbox.setTag("" + groupPosition + "," + childPosition);
chkbox.setOnCheckedChangeListener(checkboxListener);
chkbox.setText(children[groupPosition][childPosition]);
chkbox.setChecked(checkboxStatus.get(groupPosition).get(childPosition));
return chkbox;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
private OnCheckedChangeListener checkboxListener = new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String positions = buttonView.getTag().toString();
int groupPosition = Integer.valueOf(positions.split(",")[0]);
int childPosition = Integer.valueOf(positions.split(",")[1]);
checkboxStatus.get(groupPosition).set(childPosition, isChecked);
}
};
}