Android ResourcCursorAdapter的使用

关于 ResourceCursorAdapter的使用,和我上一篇的CursorAdapter的使用没有太大的区别,这里我就用拿来主义了。

正文

  一、结构

public abstract class ResourceCursorAdapter extends CursorAdapter

        

java.lang.Object

         android.widget.BaseAdapter

                   android.widget.CursorAdapter

                            android.widget.ResourceCursorAdapter

 

直接子类

         SimpleCursorAdapter

 

  二、概述

    这是一个简单的适配器,通过指定一个定义了视图UIXML文件来创建视图。

 

  三、构造函数

         public ResourceCursorAdapter (Context context, int layout, Cursor c)

           构造函数

                  参数

                   Context    ListView相关的正在运行的 SimpleListItemFactory上下文

                   layout       一个定义了列表项视图的布局文件资源ID,这个布局同时定义列表项视图和下拉视图,直到你重写它们。

                   c                获取数据的游标

 

              public ResourceCursorAdapter (Context context,int layout, Cursor c, boolean autoRequery)

    构造函数

             参数

                      Context    ListView相关的正在运行的 SimpleListItemFactory上下文

                      layout       一个定义了列表项视图的布局文件资源ID,这个布局同时定义列表项视图和下拉视图,直到你重写它们。

                      c                获取数据的游标

                      autoRequery    如果此参数为true,当适配器的数据发生变化的时,适配器会调用游标的requery()方法,使最新的数据始终显示。

 

  四、公共方法

         public View newDropDownView (Context context, Cursor cursor, ViewGroup parent)

   生成一个新的下拉视图来控制游标指向的数据

                  参数

                            context    应用程序全局信息接口(应用上下文)

                            cursor       获取数据的游标,它已经移动到正确的位置

                            parent      与新视图相关联的上级视图

                   返回值

                            新创建的视图

 

         public View newView (Context context, Cursor cursor, ViewGroup parent)

         根据指定的xml文件创建视图

                   参数

                            context    应用程序全局信息接口(应用上下文)

                            cursor       获取数据的游标,它已经移动到正确的位置

                            parent      与新视图相关联的上级视图

                   返回值

                            新创建的视图

                   参见

                            newView(android.content.Context, android.database.Cursor, ViewGroup)

 

         public void setDropDownViewResource (int dropDownLayout)

         设置下拉视图相应的布局资源

                   参数

                            dropDownLayout     用于创建下拉视图的布局资源

 

         public void setViewResource (int layout)

  设置列表项视图相应的布局资源

                   参数

                            layout       用于创建列表项视图的布局资源

示例:

public class QuickContactsDemo extends ListActivity {
    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
            Contacts._ID, // 0
            Contacts.DISPLAY_NAME, // 1
            Contacts.STARRED, // 2
            Contacts.TIMES_CONTACTED, // 3
            Contacts.CONTACT_PRESENCE, // 4
            Contacts.PHOTO_ID, // 5
            Contacts.LOOKUP_KEY, // 6
            Contacts.HAS_PHONE_NUMBER, // 7
    };

    static final int SUMMARY_ID_COLUMN_INDEX = 0;
    static final int SUMMARY_NAME_COLUMN_INDEX = 1;
    static final int SUMMARY_STARRED_COLUMN_INDEX = 2;
    static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 3;
    static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 4;
    static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 5;
    static final int SUMMARY_LOOKUP_KEY = 6;
    static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 7;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                + Contacts.DISPLAY_NAME + " != '' ))";
        Cursor c =
                getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
                null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
        startManagingCursor(c);
        ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.quick_contacts, c);
        setListAdapter(adapter);

    }

    private final class ContactListItemAdapter extends ResourceCursorAdapter {
        public ContactListItemAdapter(Context context, int layout, Cursor c) {
            super(context, layout, c);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            final ContactListItemCache cache = (ContactListItemCache) view.getTag();
            TextView nameView = cache.nameView;
            QuickContactBadge photoView = cache.photoView;
            // Set the name
            cursor.copyStringToBuffer(SUMMARY_NAME_COLUMN_INDEX, cache.nameBuffer);
            int size = cache.nameBuffer.sizeCopied;
            cache.nameView.setText(cache.nameBuffer.data, 0, size);
            final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
            final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
            cache.photoView.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            View view = super.newView(context, cursor, parent);
            ContactListItemCache cache = new ContactListItemCache();
            cache.nameView = (TextView) view.findViewById(R.id.name);
            cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
            view.setTag(cache);

            return view;
        }
    }

    final static class ContactListItemCache {
        public TextView nameView;
        public QuickContactBadge photoView;
        public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值