4

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?

Nazerke
  • 2,098
  • 7
  • 37
  • 57

1 Answers1

4

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:

    Searchable Configuration | Android Developers

    Creating a Search Interface | Android Developers

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

kris larson
  • 30,387
  • 5
  • 62
  • 74
  • Which way is the easiest to implement? Also is there specific reason the guide insists on opening up new Activity for showing search results? I want results to be on the same fragment where the search view is. And I'm using CursorLoader and CursorLoader implementation for querying and showing suggestions. I've fiddled with the 2nd way for couple days now. But can't make it happen fully. May be I should resort on the 1st way, but it seems like a lot of work – Nazerke Aug 24 '16 at 03:59
  • 1
    It's probably a little easier to do it without the search framework, but that doesn't mean it's easy. The reason Google has the concept of a searchable activity is so that another app can invoke that activity with a search term to get the search results. Since you aren't interested in opening up the search to outside apps, you are probably on the right track. If you are having problems, ask new question, describe the exact problem you're wrestling with and share your code. The SO community can help you from there. – kris larson Aug 24 '16 at 11:25
  • could you please have a look at the related problem http://stackoverflow.com/questions/39137353/searchview-implementation-via-customadapter-and-customloader-swapcursor-is-givi – Nazerke Aug 25 '16 at 05:37