I'm a bit confused by what seems as contradiction in the developer's guide. In the section "Decide if you need ContentProvider" it first states that you do if "You want to provide custom search suggestions using the search framework." and right after that it says "You don't need a provider to use an SQLite database if the use is entirely within your own application. " What if the data I'm providing for SearchView is in the SQLite database and I don't plan to share this data to any other application other than this application itself?
What if the data I'm providing for SearchView is in the SQLite database and I don't plan to share this data to any other application other than this application itself?
Then you have a choice:
Create a ContentProvider
and use the search framework. If you decide to create a ContentProvider
, you can configure the search suggestions in XML.
Here's an example from the legacy SearchableDictionary
sample application:
First you need a "searchable activity". This is declared in the manifest like this:
<application ... >
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
...
</application>
Note the searchable resource specified in the metadata. It might look like this:
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchSettingsDescription="@string/settings_description"
android:searchSuggestAuthority="com.example.android.searchabledict.DictionaryProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://com.example.android.searchabledict.DictionaryProvider/dictionary"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="1"
android:includeInGlobalSearch="true"
>
</searchable>
then the SearchView
is set up like this:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
The other benefits from doing this are that a) you can have the ContentProvider
notify you when any updates have been made to a specific content URI and b) if you eventually decide to make your data shareable with other apps, you already have most of the work done.
For further info, see these links:
Don't use the search framework. You don't need to have a ContentProvider
for search suggestions, but you'll still need to do some setup with the code to get your search and suggestions working. You'll need to supply a CursorAdapter
to the SearchView
with setSuggestionsAdapter()
, and you'll need to set up an OnQueryTextListener
to trigger the filtering on the CursorAdapter
.
Also, you'll have to write code to start the searchable activity with startActivity
when the either Enter is pressed in the SearchView
or a suggestion is selected from the suggestion list.
There's no right or wrong way, just different approaches. You have to look at both strategies and decide which one best fits the design goals of your app.