Android.widget.CursorAdapter
它首先實現了兩個接口
Filterable,CursorFilter.CursorFilterClient。
其中Filterable接口定義了getFilter()這個接口。
CursorFilterClient定義如下接口:
CharSequence convertToString(Cursor cursor);
Cursor runQueryOnBackgroundThread(CharSequence constraint);
Cursor getCursor();
void changeCursor(Cursor cursor);
注意:CursorFilter.CursorFilterClient是非public的,所以只有文檔中看不到它,在源碼中才能看到它。
CursorAdapter.java文件如下
package android.widget;
-----------------------------------省略---------------------------------
public abstract class CursorAdapter extends BaseAdapter implements Filterable,
CursorFilter.CursorFilterClient {
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected boolean mDataValid;
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected boolean mAutoRequery;
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected Cursor mCursor;
-----------------------------------省略---------------------------------
protected CursorFilter mCursorFilter;
/**
* This field should be made private, so it is hidden from the SDK.
* {@hide}
*/
protected FilterQueryProvider mFilterQueryProvider;
-----------------------------------省略---------------------------------
public Cursor getCursor() {
return mCursor;
}
-----------------------------------省略---------------------------------
public void changeCursor(Cursor cursor) {
-----------------------------------省略---------------------------------
}
-----------------------------------省略---------------------------------
public CharSequence convertToString(Cursor cursor) {
return cursor == null ? "" : cursor.toString();
}
-----------------------------------省略---------------------------------
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (mFilterQueryProvider != null) {
return mFilterQueryProvider.runQuery(constraint);
}
return mCursor;
}
public Filter getFilter() {
if (mCursorFilter == null) {
mCursorFilter = new CursorFilter(this);
}
return mCursorFilter;
}
-----------------------------------省略---------------------------------
public FilterQueryProvider getFilterQueryProvider() {
return mFilterQueryProvider;
}
-----------------------------------省略---------------------------------
public void setFilterQueryProvider(FilterQueryProvider filterQueryProvider) {
mFilterQueryProvider = filterQueryProvider;
}
-----------------------------------省略---------------------------------
}
CursorFilter文件如下:
package android.widget;
import android.database.Cursor;
class CursorFilter extends Filter {
CursorFilterClient mClient;
interface CursorFilterClient {
CharSequence convertToString(Cursor cursor);
Cursor runQueryOnBackgroundThread(CharSequence constraint);
Cursor getCursor();
void changeCursor(Cursor cursor);
}
CursorFilter(CursorFilterClient client) {
mClient = client;
}
@Override
public CharSequence convertResultToString(Object resultValue) {
return mClient.convertToString((Cursor) resultValue);
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Cursor cursor = mClient.runQueryOnBackgroundThread(constraint);
FilterResults results = new FilterResults();
if (cursor != null) {
results.count = cursor.getCount();
results.values = cursor;
} else {
results.count = 0;
results.values = null;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Cursor oldCursor = mClient.getCursor();
if (results.values != null && results.values != oldCursor) {
mClient.changeCursor((Cursor) results.values);
}
}
}
關於Filter的更多內容參見《關鍵字過濾器Filter》。
如何使用Filter呢?
調用CursorAdapter的getFilter方法得到一個Filter,調用它的filter (CharSequence constraint)方法。其中constraint就是關鍵字。
注意1:如果不給CursorAdapter設置FilterQueryProvider(通過setFilterQueryProvider方法),
那麼就需要重載Cursor runQueryOnBackgroundThread(CharSequence constraint)來實現對數據庫的過濾查詢。
當然FilterQueryProvider其實就是在它唯一的方法public abstract Cursor runQuery (CharSequence constraint)裡面實現對數據庫的過濾查詢。
參考源代碼可以知道如果提供了FilterQueryProvider,那麼CursorAdapter就是在runQueryOnBackgroundThread方法中直接調用FilterQueryProvider的runQuery方法來進行數據庫的過濾查詢