SearchManager - 添加自定义建议 [英] SearchManager - adding custom suggestions

查看:29
本文介绍了SearchManager - 添加自定义建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在线阅读了所有关于构建搜索界面和添加自定义建议的文档……但我仍然不清楚这是如何工作的.该文档说我必须为您的建议构建一个表(例如在 SQLiteDatabase 中)并使用所需的列格式化该表".我假设系统最终会自己用适当的建议填充这个表......但是哪个进程/类对此负责,以及实际插入何时发生(在用户进行任何查询之前,在用户进行了查询等)?

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

虽然我在这里问一个问题,但如果有人能澄清 AutoCompleteTextView 和带有自定义建议的 SearchView 之间的区别...真棒.与 SearchView 相比,AutoCompleteTextView 似乎很容易实现(这需要对 ContentProvider、SQLiteDatabase 帮助器类等进行更改).

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.).

推荐答案

您必须创建一个内容提供程序,它根据目前在搜索视图中输入的查询提供您的自定义建议.在 searchable.xml 中配置搜索表达式的最小长度,在征求建议之前必须达到该长度.这个内容提供者称为建议提供者(它仍然扩展 ContentProvider).内容提供者的权限也在 searchable.xml 中配置.

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.

建议提供者计算其建议的方式没有限制.您可以搜索网络查询数据库或读取文件.但是查询的答案是表格的格式.如果直接从数据库查询建议,您可以使用由数据库查询回答的游标在内容提供者的 query() 方法中传递结果.如果结果是从一个或多个源计算得出的,您可以使用 MatrixCursor 即时创建表格.

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

此处更详细地描述了搜索:http://developer.android.com/guide/topics/search/index.html

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

这篇关于SearchManager - 添加自定义建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆