SearchManager - adding custom suggestions

不羁岁月 提交于 2019-12-04 04:21:03

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!