我有一个简单的搜索小部件,我很难在我的应用程序中实现自定义搜索建议功能。
关于" Building a suggestion table"的Google Api文档记录真的很糟糕。到目前为止,我已经设法获得我的" SearchContentProvider"工作,返回建议,但当我输入下拉菜单时,我只得到"空白"字段,没有文字。
有人可以建议我如何构建建议表吗?我应该在哪里实施" SUGGEST_COLUMN_TEXT_1"为了格式化并得到我的建议吗?
SearchContentProvider.java:
package com.exploreca.tourfinder;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;
public class SearchContentProvider extends ContentProvider {
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
Cursor cursor;
private String LOGTAG;
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
//instantieting the datasouce
dbhelper = new ToursDBOpenHelper(getContext());
database = dbhelper.getReadableDatabase();
Log.i(LOGTAG, "Selection Args: " + selectionArgs[0] + "\n " + "selection is: " + selection);
cursor = database.query(ToursDBOpenHelper.TABLE_TOURS, null,
selection, selectionArgs, null, null, null);
Log.i(LOGTAG, "Returned" + cursor.getCount() + "rows");
if (cursor.getCount() > 0){
while(cursor.moveToNext()){
cursor.getColumnIndex(cursor.getString(cursor.getColumnIndex(ToursDBOpenHelper.COLUMN_CITY)));
Log.i(LOGTAG, "Cursor is" + cursor);
}
}
return cursor;
}
@Override
public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
// TODO Auto-generated method stub
return 0;
}
}
答案 0 :(得分:2)
如果您仍在寻找解决方案,则需要使用SearchManager.SUGGEST_COLUMN_TEXT_1而不是SUGGEST_COLUMN_TEXT_1作为said in this answer。
答案 1 :(得分:0)
我已经弄清楚了。即使我添加了" SUGGEST_COLUMN_TEXT_1"作为我桌子的新栏目,无论我把它放进去,它都是空的。
所以我从数据库表中删除了它,并以HashMap的形式将其添加到我的ContentProvider中,HashMap由一组键和值对组成。在HashMap中,每个键都映射到一个值。
我已经花了几天时间搞清楚这一点,希望用工作代码帮助其他人:
...
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(ToursDBOpenHelper.TABLE_TOURS);
HashMap<String, String> columnMap = new HashMap<String, String>();
columnMap.put(BaseColumns._ID, ToursDBOpenHelper.COLUMN_ID + " AS " + BaseColumns._ID);
columnMap.put(SearchManager.SUGGEST_COLUMN_TEXT_1, ToursDBOpenHelper.COLUMN_TITLE + " AS " + SearchManager.SUGGEST_COLUMN_TEXT_1);
columnMap.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA, ToursDBOpenHelper.COLUMN_ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA);
builder.setProjectionMap(columnMap);
dbhelper = new ToursDBOpenHelper(getContext());
//database = dbhelper.getReadableDatabase();
SQLiteDatabase db = dbhelper.getWritableDatabase();
cursor = builder.query(db, projection, selection, selectionArgs, null, null, sortOrder, null);
return cursor;
}
...