您的位置:首页 > 其它

其他两种绑定ListView数据的方式

2014-11-16 16:35 295 查看
1.使用ArrayAdapter绑定数据

setContentView(R.layout.activity_main);

ListView mListView = (ListView) findViewById(R.id.listview);
String[] textArray = {"功能1","功能2","功能3","功能4","功能5","功能6","功能7","功能8"};

/*
* 定义数据适配器
* android.R.layout.simple_list_item_1  Listview的子条目显示的布局的id
* textArray 显示在ListView列表中的数据
*/
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, 
textArray);

mListView.setAdapter(adapter);

2.使用simpleAdapter绑定listview数据

setContentView(R.layout.activity_main);

ListView mListView = (ListView) findViewById(R.id.listview);

List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();

Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "张三1");
map.put("icon", R.drawable.f007);
data.add(map);

map = new HashMap<String, Object>();
map.put("name", "张三2");
map.put("icon", R.drawable.f007);
data.add(map);

map = new HashMap<String, Object>();
map.put("name", "张三3");
map.put("icon", R.drawable.f007);
data.add(map);

map = new HashMap<String, Object>();
map.put("name", "张三4");
map.put("icon", R.drawable.f007);
data.add(map);

map = new HashMap<String, Object>();
map.put("name", "张三5");
map.put("icon", R.drawable.f007);
data.add(map);

SimpleAdapter adapter = new SimpleAdapter(
this, // 上下文
data, // listView绑定的数据
R.layout.listview_item, // listview的子条目的布局的id
new String[]{"name", "icon"},
// data数据中的map集合里的key
new int[]{R.id.tv_name, R.id.iv_icon}); // resource 中的id

mListView.setAdapter(adapter);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: