三木社区

标题: android API(3.1 r1)android.widget.ResourceCursorAdapter [打印本页]

作者: frankxu    时间: 2017-8-23 15:48
标题: android API(3.1 r1)android.widget.ResourceCursorAdapter
结构
继承关系
public abstract class ResourceCursorAdapter extends CursorAdapter
         
java.lang.Object
         android.widget.BaseAdapter
                   android.widget.CursorAdapter
                            android.widget.ResourceCursorAdapter
子类及间接子类
直接子类
         SimpleCursorAdapter
类概述
这是一个简单的适配器,通过指定一个定义了视图UI的XML文件来创建视图。

构造函数
       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       用于创建列表项视图的布局资源

补充
文章精选
         ListActivity简介
代码示例(ApiDemos\src\com\example\android\apis\app\QuickContactsDemo.java)
         Java:
         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);
    }
}






欢迎光临 三木社区 (http://www.sanmulink.com/) Powered by Discuz! X3.3