正文
一、結構
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檔案建立視圖
參數
參見
public void setDropDownViewResource (int dropDownLayout)
設定下拉視圖相應的布局資源
dropDownLayout 用于建立下拉視圖的布局資源
public void setViewResource (int layout)
設定清單項視圖相應的布局資源
layout 用于建立清單項視圖的布局資源
五、補充
文章精選
代碼示例(ApiDemos\src\com\example\android\apis\app\QuickContactsDemo.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));
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);
}
本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/582425,如需轉載請自行聯系原作者