我正在我的应用中处理本地搜索建议。使用与Searchable Activity良好交互的Search Widget创建搜索界面。
当我修改代码以显示建议时,问题就开始了。 Content Provide子类创建为:
public class SearchSuggester extends ContentProvider {
@Override
public boolean onCreate() {
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if(selectionArgs!=null && selectionArgs.length>0 && selectionArgs[0]!=null)
{
// do Something to return cursor
return new SQLiteIntegrator(getContext()).getSearchMatch(selectionArgs[0]);
}
else {
return null;
}
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
SQLiteIntegrator是一个与数据库交互的类。
public class SQLiteIntegrator extends SQLiteOpenHelper {
Context mContext;
public SQLiteIntegrator(Context applicationContext)
{
super(applicationContext, "SuggestionDB.db", null, 2);
this.mContext=applicationContext;
}
public Cursor getSearchMatch(String catName)
{
SQLiteDatabase database=getReadableDatabase();
String pluralSearch="UPPER(cat_name) like '% "+catName.toUpperCase()+"s %' OR UPPER(cat_name) like '"+catName.toUpperCase()+"s %' OR UPPER(cat_name) like '% "+catName.toUpperCase()+"s' OR UPPER(cat_name)='"+catName.toUpperCase()+"s'";
String searchQuery="Select * from CatSuggestion where UPPER(cat_name) like '% "+catName.toUpperCase()+" %' OR UPPER(cat_name) like '"+catName.toUpperCase()+" %' OR UPPER(cat_name) like '% "+catName.toUpperCase()+"' OR UPPER(cat_name)='"+catName.toUpperCase()+"' OR "+pluralSearch;
return database.rawQuery(searchQuery,null);
}
该程序抛出错误:
E/SuggestionsAdapter﹕ error changing cursor and caching columns
java.lang.IllegalArgumentException: column '_id' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
at android.widget.CursorAdapter.swapCursor(CursorAdapter.java:342)
at android.widget.CursorAdapter.changeCursor(CursorAdapter.java:313)
at android.widget.SuggestionsAdapter.changeCursor(SuggestionsAdapter.java:264)
at android.widget.CursorFilter.publishResults(CursorFilter.java:67)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:282)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我正在使用数据库与表CatSuggestion有三个字段cat_id,cat_parentid,cat_name。 我没有使用SQLQueryBuilder setProjection,因为我不知道如何为现有的表。
请帮忙。