Android 如何处理Search下的suggestion query/suggestion table/Intent action详解

墙那头的看这里

Handling the suggestion query

When the Search Manager requests suggestions from your content provider, it calls your content provider's query() method. You must implement this method to search your suggestion data and return a Cursor pointing to the suggestions you deem relevant.

Here's a summary of the parameters that the Search Manager passes to your query() method (listed in order):

uri
Always a content Uri, formatted as:
content://your.authority/optional.suggest.path/SUGGEST_URI_PATH_QUERY

The default behavior is for Search Manager to pass this URI and append it with the query text. For example:

content://your.authority/optional.suggest.path/SUGGEST_URI_PATH_QUERY/puppies

The query text on the end is encoded using URI encoding rules, so you might need to decode it before performing a search.

The optional.suggest.path portion is only included in the URI if you have set such a path in your searchable configuration file with the android:searchSuggestPath attribute. This is only needed if you use the same content provider for multiple searchable activities, in which case, you need to disambiguate the source of the suggestion query.

Note: SUGGEST_URI_PATH_QUERY is not the literal string provided in the URI, but a constant that you should use if you need to refer to this path.

projection
Always null
selection
The value provided in the android:searchSuggestSelection attribute of your searchable configuration file, or null if you have not declared the android:searchSuggestSelection attribute. More about using this to get the query below.
selectionArgs
Contains the search query as the first (and only) element of the array if you have declared the android:searchSuggestSelection attribute in your searchable configuration. If you have not declared android:searchSuggestSelection, then this parameter is null. More about using this to get the query below.
sortOrder
Always null

The Search Manager can send you the search query text in two ways. The default manner is for the query text to be included as the last path of the content URI passed in the uri parameter. However, if you include a selection value in your searchable configuration's android:searchSuggestSelection attribute, then the query text is instead passed as the first element of the selectionArgs string array. Both options are summarized next.

Get the query in the Uri

By default, the query is appended as the last segment of the uri parameter (a Uri object). To retrieve the query text in this case, simply use getLastPathSegment(). For example:

String query = uri.getLastPathSegment().toLowerCase();

This returns the last segment of the Uri, which is the query text entered in the search dialog.

Get the query in the selection arguments

Instead of using the URI, you might decide it makes more sense for your query() method to receive everything it needs to perform the look-up and you want the selection and selectionArgs parameters to carry the appropriate values. In such a case, add the android:searchSuggestSelection attribute to your searchable configuration with your SQLite selection string. In the selection string, include a question mark ("?") as a placeholder for the actual search query. The Search Manager calls query() with the selection string as the selection parameter and the search query as the first element in the selectionArgs array.

For example, here's how you might form the android:searchSuggestSelection attribute to create a full-text search statement:

<?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"
   
android:searchSuggestIntentAction="android.Intent.action.VIEW"
   
android:searchSuggestSelection="word MATCH ?">
</searchable>

With this configuration, your

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值