墙那头的看这里
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):
-
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
/puppiesThe 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 theandroid: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. - Always null
-
The value provided in the
android:searchSuggestSelection
attribute of your searchable configuration file, or null if you have not declared theandroid:searchSuggestSelection
attribute. More about using this to get the query below. -
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 declaredandroid:searchSuggestSelection
, then this parameter is null. More about using this to get the query below. - Always null
uri
projection
selection
selectionArgs
sortOrder
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