Android 中文API(86)——ResourceCursorAdapter

来源:互联网 发布:网络认证系统 编辑:程序博客网 时间:2024/05/18 11:49

转自"HalZhang",http://www.cnblogs.com/halzhang

一、结构

public abstract class ResourceCursorAdapterextends CursorAdapter

java.lang.Object

android.widget.BaseAdapter

android.widget.CursorAdapter

android.widget.ResourceCursorAdapter

直接子类

SimpleCursorAdapter

  二、概述

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

  三、构造函数

  publicResourceCursorAdapter(Contextcontext, int layout,Cursorc)

构造函数

参数

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

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

c 获取数据的游标

publicResourceCursorAdapter(Context context,int layout, Cursor c, boolean autoRequery)

    构造函数

   参数

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

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

c 获取数据的游标

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

  四、公共方法

publicView newDropDownView(Context context, Cursorcursor, ViewGroup parent)

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

参数

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

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

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

返回值

新创建的视图

publicView newView(Context context, Cursorcursor, ViewGroup parent)

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

参数

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

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

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

返回值

新创建的视图

参见

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

public voidsetDropDownViewResource(int dropDownLayout)

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

参数

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

public voidsetViewResource(int layout)

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

参数

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

  五、补充

  文章精选

ListActivity简介

    代码示例(ApiDemos\src\com\example\android\apis\app\QuickContactsDemo.java

publicclass QuickContactsDemoextends ListActivity {
staticfinal 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
};

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


@Override
publicvoid 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);

}

privatefinalclass ContactListItemAdapterextends ResourceCursorAdapter {
public ContactListItemAdapter(Context context,int layout, Cursor c) {
super(context, layout, c);
}

@Override
publicvoid 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);
finallong 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;
}
}

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