我正在我的应用程序中实现自定义搜索建议。我找到了有关添加自定义建议的开发人员文档 http://developer.android.com/guide/topics/search/adding-custom-suggestions.html
到目前为止,我已经创建了一个像这样的搜索文件。
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_label"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.example.MyCustomSuggestionProvider">
</searchable>
我现在正在实施MyCustomSuggestionProvider,但我发现很难建立建议表。如何使用MatrixCursor构建建议表而不实际构建文档中提到的表?请提供代码,以便于理解。
答案 0 :(得分:2)
您可以考虑使用SQLiteQueryBuilder和ProjectionMap来实现相同的结果。以下是供您参考的代码段。
private static final HashMap<String, String> PROJECTION_MAP = new HashMap<String, String>();
static {
PROJECTION_MAP.put("_id", Symbol.COL_ID);
PROJECTION_MAP.put(SearchManager.SUGGEST_COLUMN_TEXT_1, "col1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1);
PROJECTION_MAP.put(SearchManager.SUGGEST_COLUMN_TEXT_2, "col2 AS " + SearchManager.SUGGEST_COLUMN_TEXT_2);
PROJECTION_MAP.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "_id AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables("table");
builder.setProjectionMap(PROJECTION_MAP);
return builder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
}