首先我想告诉你,在 get childcount 方法中应该返回,你想要多少父项的子项......
就像如果你想要两个子项,那么获取子项应该返回 2。
我会给你确切的工作代码..
package com.example.n_level_list;
import java.util.ArrayList;
import android.os.Bundle;
import android.animation.ArgbEvaluator;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ExpandableListView mExpandableListView;
public static ArrayList<String> mArrayList,mArrayList2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mExpandableListView=(ExpandableListView)findViewById(R.id.expandableListView1);
mArrayList=new ArrayList<String>();
mArrayList.add("first");
mArrayList.add("second");
mArrayList.add("third");
mArrayList.add("fourth");
mArrayList.add("fifth");
Log.i("chk_size", "aa"+mArrayList.size());
mArrayList2=new ArrayList<String>();
ParentLevel mParentLevel=new ParentLevel(mArrayList);
mExpandableListView.setAdapter(mParentLevel);
}
public class ParentLevel extends BaseExpandableListAdapter
{
ArrayList<String> mArrayList;
public ParentLevel(ArrayList<String> mArrayList)
{
this.mArrayList=mArrayList;
}
public ParentLevel()
{
// TODO Auto-generated constructor stub
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this);
mArrayList2.add("child_first");
mArrayList2.add("child_second");
SecondLevelAdapter mSecondLevelAdapter=new SecondLevelAdapter(mArrayList2);
SecondLevelexplv.setAdapter(mSecondLevelAdapter);
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return mArrayList2.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mArrayList.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
LayoutInflater mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=mInflater.inflate(R.layout.inflate_activity, null);
TextView mTextView=(TextView)convertView.findViewById(R.id.textView1);
mTextView.setText(mArrayList.get(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
public class CustExpListview extends ExpandableListView
{
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960,
MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter
{
ArrayList<String> mArrayList2;
public SecondLevelAdapter(ArrayList<String> mArrayList2)
{
this.mArrayList2=mArrayList2;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent)
{
return null;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mArrayList2.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LayoutInflater mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=mInflater.inflate(R.layout.inflate_activity, null);
TextView mTextView=(TextView)convertView.findViewById(R.id.textView1);
mTextView.setText(mArrayList2.get(groupPosition));
return convertView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}