![](/img/trans.png)
[英]Android: how do I get SearchView close button to return the setQueryHint()?
[英]Android SearchRecentSuggestions - suggestions do not get displayed when typing in SearchView
我有一個工作搜索小部件,並希望添加搜索歷史建議。 我遵循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); } }
在Manifest中聲明提供程序
<provider android:name=".SearchHistoryProvider" android:authorities="com.mypackage.SearchHistoryProvider"> </provider>
可搜索的配置
<?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>
將查詢保存到內容提供程序(在我的可搜索活動中)
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); } }
一切正常,除了建議實際上沒有顯示(搜索看起來與我添加它們之前相同)。 任何幫助將不勝感激!
我認為問題是,Manifest.xml中的SearchHistoryProvider聲明是錯誤的。
android:name=".SearchHistoryProvider"
寫作。$ NAME是$ DEFAULTPACKAGE。$ NAME的簡寫,所以如果您的$ DEFAULTPACKAGE是'com.myapp'並且您將.SearchHistoryProvider寫為您的提供商的android:名稱,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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.