user2230793
user2230793

Reputation: 145

How to build suggestions table for custom search suggestions?

I am implementing custom search suggestions in my application. I found the developers documentation on adding custom suggestions. http://developer.android.com/guide/topics/search/adding-custom-suggestions.html

So far i have created a searchable.xml like this.

<?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>

And i am implementing MyCustomSuggestionProvider now but i find it difficult to build the suggestions table. How to build the suggestions table using MatrixCursor without actually building the table as mentioned in the documentation? Please provide code so that it's easy to understand.

Upvotes: 1

Views: 593

Answers (1)

appsroxcom
appsroxcom

Reputation: 2821

You may consider using SQLiteQueryBuilder and ProjectionMap to achieve the same result. Here is code snippet for your reference.

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

Upvotes: 2

Related Questions