本文采用一个Demo来展示Android中ExpandableListView控件的使用,如如何在组/子ListView中绑定数据源。直接上代码如下:
程序结构图:
layout目录下的 main.xml 文件源码如下:
包com.andyidea.demo中ContactsActivity.java源码如下:
package com.andyidea.demo;
import java.util.ArrayList;
import java.util.List;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.baseExpandableListAdapter;
import android.widget.TextView;
public class ContactsActivity extends ExpandableListActivity {
List group; //组列表
List> child; //子列表
ContactsInfoAdapter adapter; //数据适配器
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //设置为无标题
setContentView(R.layout.main);
getExpandableListView().setBackgroundResource(R.drawable.default_bg);
initializeData();
getExpandableListView().setAdapter(new ContactsInfoAdapter());
getExpandableListView().setCacheColorHint(0); //设置拖动列表的时候防止出现黑色背景
}
private void initializeData(){
group = new ArrayList();
child = new ArrayList>();
addInfo("Andy",new String[]{"male","138123***","GuangZhou"});
addInfo("Fairy",new String[]{"female","138123***","GuangZhou"});
addInfo("Jerry",new String[]{"male","138123***","ShenZhen"});
addInfo("Tom",new String[]{"female","138123***","ShangHai"});
addInfo("Bill",new String[]{"male","138231***","ZhanJiang"});
}
private void addInfo(String g,String[] c){
group.add(g);
List childitem = new ArrayList();
for(int i=0;i
最后,程序运行后截图如下:
希望本文所述对大家学习Android软件编程有所帮助。



