ListView的用法

一、三种Adapter构建ListView
ListView添加条目的时候, 可以使用setAdapter(ListAdapter)方法, 常用的ListAdapter有三种:
BaseAdapter: 定义一个类继承BaseAdapter, 重写4个抽象方法, ListView的条目是由getView()方法构建出来的
SimpleAdapter: 创建SimpleAdapter对象时, 传入数据(List<Map<String, ?>>), 并指定数据的绑定关系

SimpleCursorAdapter: 创建SimpleCursorAdapter对象时, 传入一个Cursor, 指定数据的绑定关系

第一步,在layout文件夹中的main.xml中添加listview标签,设置好id;

<ListView 
    android:id="@+id/personLV"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
第二步,获取ListView并添加Adapter;
personLV = (ListView) findViewById(R.id.personLV);<span style="white-space:pre">	//获取ListView
personLV.setAdapter(new MyBaseAdapter());<span style="white-space:pre">		//给ListView添加Adapter,按照Adapter中的方法对ListView添加条目	

第三步,Adapter的实现
private class MyBaseAdapter extends BaseAdapter{	//定义Adapter(作为内部类),把每个Person对象生成一个条目,将所有条目装入ListView
  public int getCount() {
	return persons.size();			//返回ListView中要装入的条目的数量
  }
  public Object getItem(int position) {		//返回指定位置上的对象	
	return persons.get(position);
  }
  public long getItemId(int position) {		//返回条目的id
        return position;
  }
  public View getView(int position, View convertView, ViewGroup parent) {		//返回指定位置上的View,会被添加到ListView中
       View item = View.inflate(getApplicationContext(), R.layout.item, null);	//根据布局文件创建View(LinearLayout)

       TextView idTV = (TextView) item.findViewById(R.id.idTV);			//获取这个新生成的View中的TextView
       TextView nameTV = (TextView) item.findViewById(R.id.nameTV);
       TextView balanceTV = (TextView) item.findViewById(R.id.balanceTV);
			
       Person p = persons.get(position);						//根据位置获取Person对象		
			
       idTV.setText(p.getId() + "");							//给TextView设置文本
	nameTV.setText(p.getName());
	balanceTV.setText(p.getBalance() + "");
	return item;
  }		
}
也可改为使用SimpleAdapter:
personLV.setAdapter(new SimpleAdapter(this, data, R.layout.item
				, new String[]{"id","name","balance"}
				, new int[]{R.id.idTV,R.id.nameTV,R.id.balanceTV}));			
/*
 * SimpleAdapter传入指定的数据和布局文件,以及匹配关系,自动生成View,装入ListView
 * 参数1:上下文环境
 * 参数2:数据,List<Map<String,Object>>,每个Person的数据装入一个Map,将所有Map装入List
 * 参数3:布局文件的资源id
 * 参数4:Map中的key,和参数5中的id对应,将指定key的value放入View中指定id对应的组件上
 * 参数5:View中的id
 */

还可改用SimpleCursorAdapter:需要注意的是,默认查出来的主键字段都为_id,所以用query方法时当数据库字段名不为_id要取别名

<span style="white-space:pre">	</span>personLV.setAdapter(new SimpleCursorAdapter(this, R.layout.item, c //
				,new String[]{ "_id","name","balance" }//
				,new int[]{R.id.idTV,R.id.nameTV,R.id.balanceTV}));			
	/*
	 * 参数1: 上下文环境
	 * 参数2: 布局文件资源id
	 * 参数3: 包含数据的游标类对象Cursor,SQLiteDatabase类的query方法得到的就是一个Cursor对象
	 * 参数4: 游标中的列名
	 * 参数5: 条目中的组件的ID,游标中的数据就会放在这些组件上
	 */

二、监听ListView的点击

调用ListView.setOnItemClickListener(OnItemClickListener)方法注册一个监听器
在监听器的onItemClick()方法中使用 parent.getItemAtPosition(position) 方法可以获取指定条目上的数据
BaseAdapter: 返回的就是自定义的getItem()方法中返回的数据
SimpleAdapter: 返回的是一个Map, 就是创建SimpleAdapter时List中的一个Map
SimpleCursorAdapter: 返回的是一个Cursor, 这个Cursor就是创建时传入的Cursor, 但是已经通过moveToPosition()方法指定到点击的索引了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值