abe*_*ape 21 java search android search-suggestion
我有一个工作搜索小部件,并希望添加搜索历史建议.我遵循Android教程(http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html),虽然搜索仍然有效,但没有显示任何建议.这是我的代码:
内容提供商
package com.mypackage;
import android.content.SearchRecentSuggestionsProvider;
public class SearchHistoryProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = SearchHistoryProvider.class.getName();
public final static int MODE = DATABASE_MODE_QUERIES;
public SearchHistoryProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
Run Code Online (Sandbox Code Playgroud)在Manifest中声明提供程序
<provider
android:name=".SearchHistoryProvider"
android:authorities="com.mypackage.SearchHistoryProvider">
</provider>
Run Code Online (Sandbox Code Playgroud)可搜索的配置
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:searchSuggestAuthority="com.mypackage.SearchHistoryProvider"
android:searchSuggestSelection=" ?">
</searchable>
Run Code Online (Sandbox Code Playgroud)将查询保存到内容提供程序(在我的可搜索活动中)
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
SearchHistoryProvider.AUTHORITY, SearchHistoryProvider.MODE);
suggestions.saveRecentQuery(query, null);
// Collapse the search view as a search is performed
MenuItem searchItem = mMenu.findItem(R.id.search);
SearchView searchView = (SearchView) mMenu.findItem(R.id.search).getActionView();
searchItem.collapseActionView();
searchView.setQuery("", false);
// send the query to the global search activity for loading the data
Intent globalSearchIntent = new Intent(this, GlobalSearchFragmentActivity.class);
GroceryOTGUtils.copyIntentData(intent, globalSearchIntent);
globalSearchIntent.putExtra(GlobalSearchFragmentActivity.GLOBAL_SEARCH, true);
startActivity(globalSearchIntent);
}
}
Run Code Online (Sandbox Code Playgroud)一切正常,除了建议实际上没有显示(搜索看起来与我添加它们之前相同).任何帮助将不胜感激!
我认为问题在于 Manifest.xml 中 SearchHistoryProvider 的声明是错误的。
android:name=".SearchHistoryProvider"
Run Code Online (Sandbox Code Playgroud)
编写 .$NAME 是 $DEFAULTPACKAGE.$NAME 的简写,因此如果您的 $DEFAULTPACKAGE 是“com.myapp”并且您编写 .SearchHistoryProvider 作为您的 Provider 的 android:name,Android 将根本找不到它,因为它位于在 com.mypackage 中,如以下代码的第一行所示。
package com.mypackage;
import android.content.SearchRecentSuggestionsProvider;
public class SearchHistoryProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = SearchHistoryProvider.class.getName();
public final static int MODE = DATABASE_MODE_QUERIES;
public SearchHistoryProvider() {
setupSuggestions(AUTHORITY, MODE);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3303 次 |
| 最近记录: |