Android SearchRecentSuggestions - 在SearchView中输入时不会显示建议 [英] Android SearchRecentSuggestions - suggestions do not get displayed when typing in SearchView
问题描述
我有一个工作搜索小部件,想要添加搜索历史记录建议。我按照Android教程( http://developer.android.com/guide/主题/ search / adding-recent-query-suggestions.html ),虽然搜索仍然有效,但不会显示任何建议。这是我的代码:
I have a working search widget and want to add search history suggestions. I followed the Android tutorial (http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html), and while the search still works, no suggestions are being displayed. Here is my code:
-
内容提供商
Content provider
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);
}
}
在清单中声明提供程序
Declaring provider in Manifest
<provider
android:name=".SearchHistoryProvider"
android:authorities="com.mypackage.SearchHistoryProvider">
</provider>
可搜索的配置
Searchable configuration
<?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>
将查询保存到内容提供商(在我的可搜索活动中)
Saving the queries to the content provider (in my searchable Activity)
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);
}
}
一切正常,除了建议实际上没有显示(搜索看起来和我添加它们之前一样)。任何帮助将不胜感激!
Everything works fine, except the suggestions do not actually show up (the search looks the same as before I added them). Any help would be greatly appreciated!
推荐答案
我认为问题在于,SearchHistoryProvider
的声明在Manifest.xml错了。
I think the problem was, that the declaration of the SearchHistoryProvider in the Manifest.xml was wrong.
android:name=".SearchHistoryProvider"
写作。$ NAME是
$ DEFAULTPACKAGE。$ NAME的简写,所以如果你的$ DEFAULTPACKAGE是'com.myapp'
und你写的.SearchHistoryProvider as你的
提供者的android:名称,Android根本找不到它,因为它位于
com.mypackage中,因为以下代码的第一行表示。
Writing .$NAME is a shorthand for $DEFAULTPACKAGE.$NAME, so if your $DEFAULTPACKAGE is 'com.myapp' und you write .SearchHistoryProvider as the android:name of your Provider, Android will simply not find it, as it is located in com.mypackage, as the first line of the following code indicates.
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);
}
}
这篇关于Android SearchRecentSuggestions - 在SearchView中输入时不会显示建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!