When you click to expand the GroupItem, getChildrenCount() is called to return the number of children of this Group (to avoid array out-of-bounds errors); then the adapter will call getChildView().
@Override
public int getChildrenCount(int groupPosition) {
Log.d(TAG, "getChildrenCount: groupPosition " + groupPosition);
return count;
}
About getChildView(), getChild(), getGroup()
We need to overwrite getChildView() to fill the view, so we must first get the data, and the purpose of getChild() is to get the data stored in the corresponding location in the adapter. Of course, if you maintain a child data list outside of the adapter, you can also fetch data directly from this list. But getChild() doesn’t it look clearer? The same is true for getGroup(). When executing getGroupView() to populate the group view, you can easily obtain the corresponding data.
private List mChildItems;
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
Log.d(TAG, " getChildView: groupPosition" + groupPosition + ", childPosition " + childPosition);
// 可以这样:
HashMap<String, String> child = (HashMap<String, String>)getChild(groupPosition, childPosition);
Log.d(TAG, " child data from getChild():" + child.get("Sub Item"));
// 也可以这样:
Log.d(TAG, " child data from my list:" + ((List)mChildItems.get(groupPosition)).get(childPosition));
return view;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
Log.d(TAG, " getChild: groupPosition" + groupPosition + ", childPosition " + childPosition);
return super.getChild(groupPosition, childPosition);
}
Log of some method calls:
11-05 12:37:50.322: D/Exp(24030): getChildrenCount: groupPosition 1
11-05 12:37:50.325: D/Exp(24030): getChildView: groupPosition1, childPosition 0
11-05 12:37:50.325: D/Exp(24030): getChild: groupPosition1, childPosition 0
11-05 12:37:50.326: D/Exp(24030): child data from getChild():Sub Item 0
11-05 12:37:50.326: D/Exp(24030): child data from my list:{Sub Item=Sub Item 0}
11-05 12:37:50.329: D/Exp(24030): getChildView: groupPosition1, childPosition 1
11-05 12:37:50.329: D/Exp(24030): getChild: groupPosition1, childPosition 1
11-05 12:37:50.329: D/Exp(24030): child data from getChild():Sub Item 1
11-05 12:37:50.329: D/Exp(24030): child data from my list:{Sub Item=Sub Item 1}
11-05 12:37:50.331: D/Exp(24030): getChildView: groupPosition1, childPosition 2
11-05 12:37:50.331: D/Exp(24030): getChild: groupPosition1, childPosition 2
11-05 12:37:50.331: D/Exp(24030): child data from getChild():Sub Item 2
11-05 12:37:50.331: D/Exp(24030): child data from my list:{Sub Item=Sub Item 2}
Essentially, ExpandableListAdapter's getChild(), getGroup() and android.widget.Adapter's Object getItem(int position) are the same thing: "Get the data item associated with the specified position in the data set."
Reference:
A project found to answer this question: Expandable ListView in ANDROID
This answer is great stackoverflow - What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?
android.widget.ExpandableListAdapter source code link
stackoverflow - Android ExpandableListView - Looking for a tutorial
About getChildrenCount
When you click to expand the GroupItem,
getChildrenCount()
is called to return the number of children of this Group (to avoid array out-of-bounds errors); then the adapter will callgetChildView()
.About getChildView(), getChild(), getGroup()
We need to overwrite
getChildView()
to fill the view, so we must first get the data, and the purpose ofgetChild()
is to get the data stored in the corresponding location in the adapter.Of course, if you maintain a child data list outside of the adapter, you can also fetch data directly from this list.
But
getChild()
doesn’t it look clearer? The same is true forgetGroup()
. When executinggetGroupView()
to populate the group view, you can easily obtain the corresponding data.Log of some method calls:
Essentially, ExpandableListAdapter's
getChild(), getGroup()
and android.widget.Adapter'sObject getItem(int position)
are the same thing: "Get the data item associated with the specified position in the data set."Reference:
A project found to answer this question: Expandable ListView in ANDROID
This answer is great stackoverflow - What is the intent of the methods getItem and getItemId in the Android class BaseAdapter?
android.widget.ExpandableListAdapter source code link
stackoverflow - Android ExpandableListView - Looking for a tutorial