?
? ????? PHP ??? ???? ??? ?? ??
CursorAdapter
翻譯時(shí)間:2011-1-20
版本:Android 2.3 r1
結(jié)構(gòu)
繼承關(guān)系
public abstract class CusrorAdapter extends BaseAdpater implements Filterable
?
java.lang.Object
android.widget.BaseAdapter
???????? android.widget.CursorAdapter
?
子類及間接子類
直接子類
???????? ResourceCursorAdapter
間接子類
SimpleCursorAdapter
?
類概述
通過該類可用Cursor
的方式訪問數(shù)據(jù)庫,并將查詢出來的數(shù)據(jù)展示到列表視圖(ListView
)部件上。其中游標(biāo)攜帶的結(jié)果集中必須有列名為“_id”的列,否則這個(gè)類無法工作。
?
構(gòu)造函數(shù)
public CusorAdapter(Context context,Cursor c)???
???????? 構(gòu)造函數(shù)。每當(dāng)數(shù)據(jù)庫的數(shù)據(jù)發(fā)生改變時(shí),適配器將調(diào)用requery()重新查詢以顯示最新的數(shù)據(jù)。
?????????????????? 參數(shù)
???????? context??? ??? 應(yīng)用程序上下文。
c????????????????????????? 用來獲取數(shù)據(jù)的游標(biāo)(Coursor)
?
public CusorAdapter(Context context,Cursor c, boolean autoRequery)???????
構(gòu)造函數(shù)。每當(dāng)數(shù)據(jù)庫的數(shù)據(jù)發(fā)生改變時(shí),適配器將調(diào)用requery()重新查詢以顯示最新的數(shù)據(jù)。
?????????????????? 參數(shù)
???????? context??? ??? 應(yīng)用程序上下文。
c????????????????????????? 用來獲取數(shù)據(jù)的Coursor
??????????????????????????? autoRequry ??? 設(shè)置為true時(shí),每當(dāng)數(shù)據(jù)庫的數(shù)據(jù)發(fā)生改變時(shí),適配器將調(diào)用requery()重新查詢以顯示最新的數(shù)據(jù)。
?
公共方法
public abstract void bindView (View view, Context context, Cursor cursor)
重用一個(gè)已有的view,使其顯示當(dāng)前cursor所指向的數(shù)據(jù)。
參數(shù)
??????????????????????????? view? ?????????????? 已存在的視圖, 返回之前newView方法創(chuàng)建的視圖。
??????????????????????????? context? ???????? 應(yīng)用程序上下文
??????????????????????????? cursor ????????????? 用于獲取數(shù)據(jù)的Coursor。Coursor已經(jīng)移到正確的位置。
????????
public void changeCursor (Cursor cursor)
???????? 更改底層的游標(biāo)為新傳入的游標(biāo)。如果游標(biāo)已經(jīng)存在則先關(guān)閉這個(gè)已存在的游標(biāo)。
參數(shù)??????????????????
??????????????????????????? cursor ????????????? 新Cursor。
?
public CharSequence convertToString (Cursor cursor)
???????? 將cursor轉(zhuǎn)換成CharSequence。子類應(yīng)該重寫這個(gè)方法并轉(zhuǎn)換它們的結(jié)果。這個(gè)方法的默認(rèn)實(shí)現(xiàn)是:當(dāng)cursor為空時(shí)返回一個(gè)空串,否則直接返回調(diào)用cursor的toString()方法。
參數(shù)????????
??????????????????????????? cursor ????????????? 將cursor轉(zhuǎn)換成CharSequence對(duì)象。
???????? 返回值
?????????????????? 返回表示CharSequence的值。
?
public int getCount ()
???????? (譯者注:獲取適配器中數(shù)據(jù)的總行數(shù)。)
?????????????????? 參見
??????????????????????????? getCount()
?
public Cursor getCursor ()
???????? 返回當(dāng)前適配器綁定的Cursor對(duì)象。
返回值
???????? Cursor對(duì)象。
?
public View getDropDownView (int position, View convertView, ViewGroup parent)
獲取下拉列表選項(xiàng)指定位置的視圖對(duì)象
參數(shù)
???????? position? ????????????????? 視圖(View)對(duì)象的行索引。
???????? convertView? ????? 重用已有的視圖(View)對(duì)象。備注:在使用前你應(yīng)該檢查????????????????????????????????????????????????????????????? 一下這個(gè)視圖對(duì)象是否非空并且這個(gè)對(duì)象的類型是否合適??????????????????????????????????????????????????????? 。由此引伸出,如果該對(duì)象不能被轉(zhuǎn)換并顯示正確的數(shù)據(jù),?????????????????????????????????????????????????? 這個(gè)方法內(nèi)部就會(huì)重新創(chuàng)建一個(gè)合適的視圖(View)對(duì)象。
???????? parent???????????????????????? 不管是轉(zhuǎn)換后的還是重新創(chuàng)建的視圖(View)對(duì)象,始終都會(huì)依附于parent對(duì)象上。
返回值????
??????????????????????????? 返回視圖(View)對(duì)象,該對(duì)象顯示數(shù)據(jù)集指定位置上的數(shù)據(jù)。
?
public Filter getFilter ()
返回一個(gè)可以通過一種過濾模式來約束數(shù)據(jù)的過濾器。
這個(gè)方法通常在Adapter類實(shí)現(xiàn)。
返回值
??????????????????????????? 一個(gè)用于約束數(shù)據(jù)的過濾器
?
public FilterQueryProvider getFilterQueryProvider ()
返回一個(gè)提供過濾的查詢過濾器。若過濾器為null,則不再過濾。
返回值
??????????????????????????? 返回當(dāng)前過濾器對(duì)象,如果不存在返回null。
?????????????????? 參見
?????????????????? setFilterQueryProvider(android.widget.FilterQueryProvider)
?????????????????? runQueryOnBackgroundThread(CharSequence)
?
public Object getItem (int position)
(譯者注:獲取數(shù)據(jù)集中指定位置上的數(shù)據(jù)項(xiàng)目)
?????????????????? 參見
??????????????????????????? getItem(int)
?
public long getItemId (int position)
???????? (譯者注:獲取數(shù)據(jù)集中的指定位置上的行id。)
?????????????????? 參見
??????????????????????????? getItemId(int)
?
public View getView (int position, View convertView, ViewGroup parent)
(譯者注:獲取一個(gè)顯示數(shù)據(jù)集中指定位置數(shù)據(jù)段視圖??梢允謩?dòng)創(chuàng)建視圖,或者從XML設(shè)計(jì)文件填充。當(dāng)視圖從XML設(shè)計(jì)文件填充時(shí),父視圖(如GridView,ListView等)將接受默認(rèn)的設(shè)計(jì)參數(shù),除非使用inflate(int, android.view.ViewGroup, boolean)去指定一個(gè)根視圖和防止依附于根視圖。)
???????? 參見
?????????????????? getView(int, View,
ViewGroup)
?
public boolean hasStableIds ()
無論項(xiàng)ID代表的基礎(chǔ)數(shù)據(jù)的是否變化都保持不變。
返回值
如果為TRUE,意味著相同的ID始終引用相同的對(duì)象。
?
public View newDropDownView (Context context, Cursor cursor, ViewGroup parent)
生成一個(gè)新的下拉視圖來保存cursor指向的數(shù)據(jù)
???????? 參數(shù)
?????????????????? context ?應(yīng)用程序全局信息接口(應(yīng)用上下文)
cursor? ?獲取數(shù)據(jù)的游標(biāo),它已經(jīng)移動(dòng)到正確的位置
parent? 與新視圖相關(guān)聯(lián)的上級(jí)視圖
返回值
新創(chuàng)建的視圖。
?
public abstract View newView (Context context, Cursor cursor, ViewGroup parent)
新建一個(gè)視圖來保存cursor指向的數(shù)據(jù)
???????? 參數(shù)
?????????????????? context ?應(yīng)用程序全局信息接口(應(yīng)用上下文)
cursor? ?獲取數(shù)據(jù)的游標(biāo),它已經(jīng)移動(dòng)到正確的位置
parent? 與新視圖相關(guān)聯(lián)的上級(jí)視圖
返回值
??????????????????????????? 新創(chuàng)建的視圖。
?
public Cursor runQueryOnBackgroundThread (CharSequence constraint)
執(zhí)行含指定約束的查詢。此查詢依賴于適配器的過濾器。查詢是由FilterQueryProvider
提供。如果沒有指定FilterQueryProvider,當(dāng)前cursor不過濾只返回。該方法會(huì)通過changeCursor(Cursor)方法返回一個(gè)Cursor對(duì)象,并且關(guān)閉掉先前的Cursor對(duì)象。這個(gè)方法始終在后臺(tái)線程執(zhí)行,而不是在應(yīng)用程序的主線程(或是UI線程)中運(yùn)行。規(guī)定:當(dāng)參數(shù)constraint為null或?yàn)榭諘r(shí),該方法返回原始結(jié)果。
???????? 參數(shù)
?????????????????? constraint? 該查詢必須被過濾的約束。
返回值
返回含有新的查詢結(jié)果的Cursor。
參考
???????? getFilter()
getFilterQueryProvider()
?????????????????? setFilterQueryProvider(android.widget.FilterQueryProvider)
?
public void setFilterQueryProvider (FilterQueryProvider filterQueryProvider)
設(shè)置一個(gè)過濾器,用來過濾當(dāng)前的Cursor對(duì)象。當(dāng)這個(gè)適配器需要進(jìn)行過濾操作時(shí),
runQuery(CharSequence)
方法被調(diào)用
。
???????? 參數(shù)
filterQueryProvider ?過濾器對(duì)象,設(shè)置為null時(shí),就相當(dāng)于移除了該過濾器。
?????????????????? 參考
??????????????????????????? getFilterQueryProvider()
runQueryOnBackgroundThread(CharSequence)
?
受保護(hù)方法
protected void init (Context context, Cursor c, boolean autoRequery)
(譯者注:供構(gòu)造函數(shù)使用初始化相關(guān)參數(shù))
?
protected void onContentChanged ()
當(dāng)cursor對(duì)象上的ContentObserver接收到改變的通知時(shí)就會(huì)調(diào)用該方法,其默認(rèn)實(shí)現(xiàn)提供了自動(dòng)重新查詢方式,但可以被子類重寫。
?
補(bǔ)充
?????? 文章精選
?????????????????? android CursorAdapter的監(jiān)聽事件
?????????????????? 實(shí)現(xiàn)基于Android的英文電子詞典