|
顾名思义,CursorAdapater 就是adapter和cursor捆绑到了一块,便于adapter管理和使用cursor,
public class AutoCompleteAdapter extends ResourceCursorAdapter {
private final static boolean DBG = com.ninesky.browser.BrowserApp.DBG;
private final static String TAG = com.ninesky.browser.BrowserApp.TAG + “view.UrlSuggestionAdapter”;
private final static String WEB_NOT_AVAILABLE = “Web page not available”;
private final static String URL_START_ABOUT_VALUE = “about:”;
public static boolean mIsRunQueryOnBackgroundThread = false;
private BrowserActivity mContext;
public AutoCompleteAdapter (BrowserActivity context,Cursor c) {
super(context, R.layout.complete_dropdown_item, c, false);
mContext = context;
}
public void bindView(View v, Context context, Cursor cursor) {
if(DBG) Log.d(TAG,”———————————- bindView”);
TextView titleView = (TextView) v.findViewById(…);
TextView urlView = (TextView) v.findViewById(…);
ImageView iconView = (ImageView) v.findViewById(….);
.
if (urlView != null) {
String url = cursor.getString(cursor.getColumnIndex(“url”));
if (TextUtils.isEmpty(url)) {
String str = “”;
url = cursor.getString(cursor.getColumnIndex(“title”));
}
if (TextUtils.isEmpty(url)) {
urlView.setVisibility(View.INVISIBLE);
urlView.setText(“”);
} else {
urlView.setVisibility(View.VISIBLE);
urlView.setText(url);
}
}
if (iconView != null) {
String strIconName = cursor.getString(cursor.getColumnIndex(“icon”));
if (TextUtils.isEmpty(strIconName)) {
iconView.setImageBitmap(null);
} else {
iconView.setImageResource(Integer.parseInt(strIconName));
}
}
}
.
public String getTitle(int pos) {
String strTitle = “”;
Cursor c = getCursor();
if ((c != null) && (c.moveToPosition(pos))) {
strTitle = c.getString(c.getColumnIndex(“title”));
} else {
strTitle = “”;
}
return strTitle;
}
public String getUrl(int pos) {
String strUrl = “”;
Cursor c = getCursor();
if ((c != null) && (c.moveToPosition(pos))) {
strUrl = c.getString(c.getColumnIndex(“suggest_intent_data”));
if (TextUtils.isEmpty(strUrl)) {
strUrl = c.getString(c.getColumnIndex(“suggest_intent_query”));
}
}
return strUrl;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if(DBG) Log.d(TAG,”———————————- position=”+position
try {
view = super.getView(position, convertView, parent);
} catch (RuntimeException e) {
Cursor cursor = getCursor();
view = newView(mContext, cursor, parent);
if (view != null) {
((TextView) view.findViewById(R.id.TextView_title)).setText(e.getMessage());
}
return view;
}
// 后台cursor 更新线程
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (!mIsRunQueryOnBackgroundThread) {
return null;
}
if (DBG) {
Log.d(TAG, “runQueryOnBackgroundThread(” + constraint + “)”);
}
。。。。。
cursor = getSuggestions( query, 50);
if (cursor != null) {
cursor.getCount();
Log.d(TAG, “Search suggestions cursor.getCount=”+ cursor.getCount());
return cursor;
}else{
Log.d(TAG, “Search suggestions cursor==null”);
}
} catch (Exception e) {
Log.w(TAG, “Search suggestions query threw an exception.”, e);
}
return null;
}
public Cursor getSuggestions(String query, int limit) {
Log.d(TAG, “1111″);
// finally, make the query
return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
}
}
优化 mIsRunQueryOnBackgroundThread 标志位 可以控制 cursor 更新 |
|