2013-04-26 209 views
1

我在我的應用程序中實現了自定義搜索建議。我找到了有關添加自定義建議的開發者文檔。 http://developer.android.com/guide/topics/search/adding-custom-suggestions.html如何構建自定義搜索建議的建議表?

到目前爲止,我已經創建了像這樣的searchable.xml。

<?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構建建議表而不實際構建文檔中提到的表?請提供代碼以便於理解。

回答

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); 
} 
+0

謝謝。有效。 – user2230793 2013-04-26 20:42:10