import java.util.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
/*
* 本例展示了3种列表
* 1、主列表mainView:单字符串单行显示,simple_list_item_1,
* 2、子列表1,lv1:双字符串双行显示,simple_list_item_2
* 3、子列表2,lv2:自定义显示,多View
*/
public class ListViewTest extends Activity {
private ListView mainView=null;
private ListView myList1=null;
private ListView myList2=null;
private SimpleAdapter sa=null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainView_init();
setContentView(mainView);
}
/*mainView初始化*/
void mainView_init(){
mainView=new ListView(this);
mainView.setLayoutParams(new LinearLayout.LayoutParams(-1,-1));
//主列表是采用ArrayAdapter实现
String[] strList={"simple_list_item_2","自定义"};
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strList);
mainView.setAdapter(aa);
//绑定监听
mainView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int index,long arg3) {
if(index==0){
if(myList1==null){
myList1_init();
}
setContentView(myList1);
}else if(index==1){
if(myList1==null){
myList2_init();
}
setContentView(myList2);
}
}
});
}
/*sa初始化*/
void sa_init(){
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> item;
item=new HashMap<String,Object>();
item.put("姓名","张三同学");
item.put("性别","男");
list.add(item);
item=new HashMap<String,Object>();
item.put("姓名","李四同学");
item.put("性别","男");
list.add(item);
item=new HashMap<String,Object>();
item.put("姓名","小兰同学");
item.put("性别","女");
list.add(item);
//
String str[]={"姓名","性别"};
int id[]={android.R.id.text1,android.R.id.text2};//view由系统系统提供的
//
sa=new SimpleAdapter(this,list,android.R.layout.simple_list_item_2,str,id);
}
/*myList1初始化*/
void myList1_init(){
myList1=new ListView(this);
myList1.setLayoutParams(new LinearLayout.LayoutParams(-1,-1));
sa_init();
myList1.setAdapter(sa);
}
/*myList2初始化*/
void myList2_init(){
myList2=new ListView(this);
myList2.setLayoutParams(new LinearLayout.LayoutParams(-1,-1));
//自定义MyAapter,见下篇
MyAdapter ma=new MyAdapter(this);
myList2.setAdapter(ma);
}
}