如果数据不共享给其他应用程序,ContentProvider 对于 SearchView 来说是多余的吗?
Is ContentProvider redundant for SearchView if data isn't to be shared to other apps?
我对 developer's guide 中看似矛盾的地方感到有点困惑。在 "Decide if you need ContentProvider" 部分中,它首先声明如果 "You want to provide custom search suggestions using the search framework." 你会做,然后紧接着它说 "You don't need a provider to use an SQLite database if the use is entirely within your own application. " 如果我为 SearchView 提供的数据在 SQLite 数据库中,我不知道怎么办?不打算将此数据共享给除此应用程序本身以外的任何其他应用程序吗?
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?
那么你有一个选择:
创建ContentProvider
并使用搜索框架。如果您决定创建ContentProvider
,您可以配置搜索XML 中的建议。
这是来自遗留 SearchableDictionary
示例应用程序的示例:
首先你需要一个"searchable activity"。这在清单中声明如下:
<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>
注意元数据中指定的可搜索资源。它可能看起来像这样:
<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>
那么SearchView
就是这样设置的:
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
这样做的其他好处是 a) 您可以让 ContentProvider
在对特定内容 URI 进行任何更新时通知您,并且 b) 如果您最终决定让您的数据与其他应用程序共享,那么您已经完成了大部分工作。
有关更多信息,请参阅以下链接:
不要使用搜索框架。您不需要 ContentProvider
搜索建议,但您仍然需要对代码进行一些设置才能使您的搜索和建议正常工作。您需要使用 setSuggestionsAdapter()
向 SearchView
提供 CursorAdapter
,并且您需要设置 OnQueryTextListener
以触发对 [=19= 的过滤].
此外,当在 SearchView
中按下 Enter 或从建议中选择建议时,您必须编写代码以使用 startActivity
启动可搜索的 activity列表.
没有正确或错误的方法,只有不同的方法。您必须查看这两种策略并决定哪一种最适合您应用的设计目标。
我对 developer's guide 中看似矛盾的地方感到有点困惑。在 "Decide if you need ContentProvider" 部分中,它首先声明如果 "You want to provide custom search suggestions using the search framework." 你会做,然后紧接着它说 "You don't need a provider to use an SQLite database if the use is entirely within your own application. " 如果我为 SearchView 提供的数据在 SQLite 数据库中,我不知道怎么办?不打算将此数据共享给除此应用程序本身以外的任何其他应用程序吗?
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?
那么你有一个选择:
创建
ContentProvider
并使用搜索框架。如果您决定创建ContentProvider
,您可以配置搜索XML 中的建议。这是来自遗留
SearchableDictionary
示例应用程序的示例:首先你需要一个"searchable activity"。这在清单中声明如下:
<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>
注意元数据中指定的可搜索资源。它可能看起来像这样:
<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>
那么
SearchView
就是这样设置的:SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setIconifiedByDefault(false);
这样做的其他好处是 a) 您可以让
ContentProvider
在对特定内容 URI 进行任何更新时通知您,并且 b) 如果您最终决定让您的数据与其他应用程序共享,那么您已经完成了大部分工作。有关更多信息,请参阅以下链接:
不要使用搜索框架。您不需要
ContentProvider
搜索建议,但您仍然需要对代码进行一些设置才能使您的搜索和建议正常工作。您需要使用setSuggestionsAdapter()
向SearchView
提供CursorAdapter
,并且您需要设置OnQueryTextListener
以触发对 [=19= 的过滤].此外,当在
SearchView
中按下 Enter 或从建议中选择建议时,您必须编写代码以使用startActivity
启动可搜索的 activity列表.
没有正确或错误的方法,只有不同的方法。您必须查看这两种策略并决定哪一种最适合您应用的设计目标。