ArrayAdapter绑定ListView :
String[] names = { "fsd", "fds", "rew" };
ListView lView = new ListView(this);
//R.array.colors引用定义在value下的数组
ArrayAdapter adapter = new ArrayAdapter(this,
R.array.colors,android.R.layout.simple_list_item_1);
lView.setAdapter(adapter);
这里使用代码绑定一个数组,也可以使用xml文件来绑定一个数组 .
<string-array name="colors">
<item>red</item>
<item>blue</item>
<item>green</item>
<item>yellow</item>
<item>black</item>
</string-array>
ArrayAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.colors, android.R.layout.simple_list_item_1);
android.R.layout.simple_list_item_1是系统提供的一个layout。我们也可以自己定义一个layout来作为listview的一个item。
SimpleAdapter:
List> list = new ArrayList>();
Map map = new HashMap();
map.put("name", "ywm");
map.put("sex", "man");
list.add(map);
map = new HashMap();
map.put("name", "zj");
map.put("sex", "woman");
list.add(map);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, new String[] { "name",
"sex" }, new int[] { android.R.id.text1,
android.R.id.text2 });
lView.setAdapter(simpleAdapter);
显示效果如图: