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

1.      ArrayAdapter:简单的listView,只有TextVeiw时使用

2.     SimpleAdapter:


ArrayAdapter:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		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);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

SimpleAdapter:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		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,item中的内容,布局;
				new String[]{"name", "icon"}, 	// data数据中的map集合里的key
				new int[]{R.id.tv_name, R.id.iv_icon}); // resource 中的id
		
		mListView.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值