Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SearchManager - adding custom suggestions

I've read all of the documentation online about building search interfaces and adding custom suggestions... but I'm still unclear on how this works. The documentation says that I must "Build a table (such as in an SQLiteDatabase) for your suggestions and format the table with required columns". I'm assuming the system will eventually fill this table with the appropriate suggestions on its own... but which process/class is responsible for this, and when will the actual insertions occur (before any query is made by the user, after a query has been made by the user, etc.)?

And while I'm asking a question up here, if someone could clarify the difference between an AutoCompleteTextView and a SearchView w/ custom suggestions... that'd be awesome. AutoCompleteTextView seems suspiciously easy to implement compared to the SearchView (which requires changes to be made to the ContentProvider, SQLiteDatabase helper class, etc.).

like image 907
Alex Lockwood Avatar asked Jan 29 '12 07:01

Alex Lockwood


1 Answers

You have to create a content provider which delivers your custom suggestions based on a query so far entered in the search view. In your searchable.xml you configure the minimum length of the search expression, which must be reached before asking for suggestions. This content provider is called a suggestion provider (it still extends ContentProvider). The content provider's authority is also configured in searchable.xml.

There is no limitation on how the suggestion provider computes its suggestions. You can search the web query a database or read a file. But the answer to the query is in the format of a table. If the suggestions is directly queried from a database you can use the cursor answered by the database query to deliver the result in the content provider's query() method. If the result is computed from one or more sources you can create a table on the fly by using a MatrixCursor.

The rows of the answer from the suggestion provider are used by the search mechanism to display the suggestion, they are stored in a table. The format of the rows is as follows:

private static final String[] COLUMNS = {
    "_id",
    SearchManager.SUGGEST_COLUMN_ICON_1,        // ID of a drawable (icon) as String
    SearchManager.SUGGEST_COLUMN_TEXT_1,        // main text for suggestion display
    SearchManager.SUGGEST_COLUMN_TEXT_2,        // secondary text for suggestion display
    SearchManager.SUGGEST_COLUMN_INTENT_DATA,   // this could be an URI to access the suggestion as used in an intent with a VIEW action
    SearchManager.SUGGEST_COLUMN_INTENT_ACTION, // this could be Intent.ACTION_VIEW
    SearchManager.SUGGEST_COLUMN_SHORTCUT_ID    // e.g. SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT
};

Searching is described here in more detail: http://developer.android.com/guide/topics/search/index.html

like image 198
Stefan Avatar answered Nov 11 '22 16:11

Stefan