Android SearchRecentSuggestions – 在SearchView中input时,不会显示build议

我有一个工作search小部件,并希望添加search历史build议。 我遵循Android教程( http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html ),而search仍然有效,没有任何build议正在显示。 这是我的代码:

  1. 内容提供商

    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); } } 
  2. 在Manifest中声明提供者

     <provider android:name=".SearchHistoryProvider" android:authorities="com.mypackage.SearchHistoryProvider"> </provider> 
  3. 可search的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> 
  4. 将查询保存到内容提供者(在我的可search活动中)

     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); } } 

一切工作正常,除了build议没有实际显示(search看起来像以前一样,我添加)。 任何帮助将不胜感激!

Solutions Collecting From Web of "Android SearchRecentSuggestions – 在SearchView中input时,不会显示build议"

我认为问题是,Manifest.xml中SearchHistoryProvider的声明是错误的。

 android:name=".SearchHistoryProvider" 

编写。$ NAME是$ DEFAULTPACKAGE。$ NAME的简写forms,所以如果你的$ 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); } }