安卓绑定数据到listview,简单版

//---------下面是绑定数据到listview-----------
//生成一个ArrayList类型的变量list 
	ListView listview=(ListView)this.findViewById(R.Id.list);
        ArrayList<HashMap<String, String>> list =new ArrayList<HashMap<String,String>>(); 
        //生成两个HashMap类型的变量map1 , map2 
        //HashMpa为键值对类型。第一个参数为建,第二个参数为值 
        HashMap<String, String> map1 =new HashMap<String, String>(); 
        HashMap<String, String> map2 =new HashMap<String, String>(); 
        //把数据填充到map1和map2中。 
        map1.put("user_name", "张三"); 
        map1.put("user_ip", "192.168.1.52"); 

        map2.put("user_name", "李四"); 
        map2.put("user_ip", "192.168.0.1"); 
        //把map1和map2添加到list中 
        list.add(map1); 
        list.add(map2); 
        //生成一个SimpleAdapter类型的变量来填充数据 
        SimpleAdapter listAdapter =new SimpleAdapter(this, list, R.layout.row, new String[]{"user_name" , "user_ip"}, new int[]{R.id.row_name , R.id.row_ip}); 
        //设置显示的ListView 
        listview.setAdapter(listAdapter);

	//--------------绑定结束------------------
        //=-----------绑定item的点击事件
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        		HashMap<String,String> map = (HashMap<String, String>) parent.getItemAtPosition(position);
        		Toast.makeText(getApplicationContext(),map.get("user_name") ,Toast.LENGTH_SHORT).show();
        	}
	});
//-------------------绑定结束-------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值