先看一下效果截图: 代码如下: package gyf.google; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.string; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.Button; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; public class g2 extends Activity { /** Called when the activity is first created. */ private List<Map<String, Object>> group=new ArrayList<Map<String,Object>>(); private List<List<Map<String, Object>>> child=new ArrayList<List<Map<String,Object>>>(); public void initList(){ //////////////////group添加数据 Map<String, Object> map=new HashMap<String, Object>(); map.put("group", "家人"); group.add(map); map=new HashMap<String, Object>(); map.put("group", "同学"); group.add(map); map=new HashMap<String, Object>(); map.put("group", "同事"); group.add(map); ///////////////////////child添加数据 List<Map<String, Object>> itemList=new ArrayList<Map<String,Object>>(); map=new HashMap<String, Object>(); map.put("name", "老婆"); map.put("tel", "12458751102"); itemList.add(map); map=new HashMap<String, Object>(); map.put("name", "我"); map.put("tel", "15138750769"); itemList.add(map); map=new HashMap<String, Object>(); map.put("name", "妈"); map.put("tel", "15111091111"); itemList.add(map); child.add(itemList); itemList=new ArrayList<Map<String,Object>>(); map=new HashMap<String, Object>(); map.put("name", "甲"); map.put("tel", "15236485026"); itemList.add(map); map=new HashMap<String, Object>(); map.put("name", "乙"); map.put("tel", "12451368452"); itemList.add(map); child.add(itemList); itemList=new ArrayList<Map<String,Object>>(); map=new HashMap<String, Object>(); map.put("name", "丙"); map.put("tel", "14216983625"); itemList.add(map); child.add(itemList); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ExpandableListView elView=(ExpandableListView)findViewById(R.id.g1_expandableListView1); initList(); myExpandable mExpandable=new myExpandable(this, group, child); elView.setAdapter(mExpandable); } public class myExpandable extends BaseExpandableListAdapter{ Context context; List<Map<String, Object>> group=null; List<List<Map<String, Object>>> child=null; LayoutInflater inflater=null; public class holder{ TextView nameTextView; TextView telTextView; Button button; } public myExpandable(Context cActivity,List<Map<String, Object>> groupList,List<List<Map<String, Object>>> childList){ this.context=cActivity; this.group=groupList; this.child=childList; inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return child.get(groupPosition).get(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 holder h=null; if (convertView==null) { convertView=inflater.inflate(R.layout.child_layout, null); h=new holder(); h.nameTextView=(TextView)convertView.findViewById(R.id.childname_textView1); h.telTextView=(TextView)convertView.findViewById(R.id.childTel_textView2); h.button=(Button)convertView.findViewById(R.id.child_update_button); convertView.setTag(h); } else { h=(holder)convertView.getTag(); } Map<String, Object> map=child.get(groupPosition).get(childPosition); if (map.size()>0) { h.nameTextView.setText((String)map.get("name")); h.telTextView.setText((String)map.get("tel")); h.button.setOnClickListener(new myOnclickListener11(groupPosition,childPosition)); } return convertView; } class myOnclickListener11 implements OnClickListener{ private int groupPosition; private int childPosition; public myOnclickListener11(int gPosition,int cPosition){ this.groupPosition=gPosition; this.childPosition=cPosition; } @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(context, "您单击了修改", Toast.LENGTH_SHORT).show(); child.get(groupPosition).get(childPosition).put("tel", "1111111"); myExpandable.this.notifyDataSetChanged(); } } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return child.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { // TODO Auto-generated method stub return group.get(groupPosition); } @Override public int getGroupCount() { // TODO Auto-generated method stub return group.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) { // TODO Auto-generated method stub String string=(String)group.get(groupPosition).get("group"); return getGenericView(string); } @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 true; } public TextView getGenericView(String s){ AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(context); text.setLayoutParams(lp); text.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT); text.setPadding(50, 0, 0, 0); text.setText(s); return text; } } }