Adapter的使用

一、概述

         Adapter是将数据绑定到UI界面上的桥接类,比如通过它将数据填充到ListView, GridView, Gallery。

         而android 提供了几种Adapter:ArrayAdapter, BaseAdapter, CursorAdapter, HeaderViewListAdapter, ListAdapter, ResourceCursorAdapter, SimpleAdapter, SimpleCursorAdapter, SpinnerAdapter, WrapperListAdapter。根据数据来源形式的不同可以选择不同的Adapter,比如数据来源于一个Arraylist 就使用BaseAdapter,SimpleAdapter,而数据来源于通过查询数据库获得Cursor那就使用SimpleCursorAdapter

 ArrayAdapter

ArrayAdapter是一个绑定View到一组对象的通用类。默认情况下,ArrayAdapter绑定每个对象的toString值到在layout中预先定义的TextView控件上。可变通的,构造函数允许你使用更加复杂的layout或者通过重写getView方法来扩展类从而使用TextView的替代物(如ImageView或嵌套的layout)。

 

 SimpleCursorAdapter

SimpleCursorAdapter绑定ViewContent Provider查询返回的游标上。指定一个XML layout定义,然后将数据集中的每一列的值绑定到layout中的一个View上。

 

 

二、Adapter的使用

2.1 SimpleCursorAdapter的使用

使用步骤:

a、使用getContentResolver方法返回ContentResolver对象,通过查询该对象获取Cursor对象:

           Cursor cursor = getContentResolver().query(Tasks.CONTENT_URI, null, null, null, null);

             

        以上 参见ContentResolver的使用 。

        

b、创建SimpleCursorAdapter对象

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_2, cursor,
				new String[] {Tasks._ID, Tasks.CONTENT },
				new int[] { android.R.id.text1,android.R.id.text2 });

 

android.widget.SimpleCursorAdapter.SimpleCursorAdapter(Context context, int layout, Cursor c,String[] from, int[] to)

        该API已被弃用。

  This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, useandroid.app.LoaderManager with a android.content.CursorLoader.

Constructor the enables auto-requery.

 

 

c、调用ListActivity的setListAdapter方法显示列表:

            setListAdapter(adapter);

 

 

 

三、深入理解Adapter

 

 

 

参考资料:

http://blog.chinaunix.net/uid-25547034-id-3075307.html

http://yahaitt.iteye.com/blog/453060

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值