简体   繁体   English

Android SearchRecentSuggestions - 在SearchView中输入时不会显示建议

[英]Android SearchRecentSuggestions - suggestions do not get displayed when typing in SearchView

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. 我遵循Android教程( http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html ),虽然搜索仍然有效,但没有显示任何建议。 Here is my code: 这是我的代码:

  1. 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); } } 
  2. Declaring provider in Manifest 在Manifest中声明提供程序

     <provider android:name=".SearchHistoryProvider" android:authorities="com.mypackage.SearchHistoryProvider"> </provider> 
  3. 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> 
  4. 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! 任何帮助将不胜感激!

I think the problem was, that the declaration of the SearchHistoryProvider in the Manifest.xml was wrong. 我认为问题是,Manifest.xml中的SearchHistoryProvider声明是错误的。

android:name=".SearchHistoryProvider"

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. 写作。$ 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM