Android创建ListView使用两种适配器的简单方法
1、使用ArrayAdapter:
String[] array = { "aaaaaaaaaaa", "bbbbbbbbb", "ccccccccc", "ddddd",
"eeeee", "fffffffff" };
ListView listView = new ListView(this);
listView.setItemsCanFocus(true);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//可多选
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, array);
listView.setAdapter(adapter);
setContentView(listView);
2、使用SimpleAdapter :
ListView listView = new ListView(this);
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> item = new HashMap<String, Object>();
item.put("姓名", "张三");
item.put("性别", "男");
data.add(item);
item = new HashMap<String, Object>();
item.put("姓名", "李三");
item.put("性别", "男");
data.add(item);
item = new HashMap<String, Object>();
item.put("姓名", "张六");
item.put("性别", "女");
data.add(item);
SimpleAdapter adapter = new SimpleAdapter(this, data,
android.R.layout.simple_list_item_2, new String[] { "姓名" ,"性别"},
new int[] { android.R.id.text1 , android.R.id.text2 });
listView.setAdapter(adapter);
setContentView(listView);