【问题标题】:Search history in Android quick searchAndroid快速搜索中的搜索历史
【发布时间】:2011-05-11 21:11:00
【问题描述】:

我正在自定义快速搜索以显示来自我的应用程序的数据。它工作正常。现在的问题是,当我点击搜索按钮时,我无法看到搜索历史。我应该怎么做才能获得搜索历史(以前搜索过的关键字)?

【问题讨论】:

    标签: android history quick-search


    【解决方案1】:

    如果你浏览 developer.android.com 上的教程,我想你会找到你要找的东西:

    http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html

    诀窍是实现一个扩展 SearchRecentSuggestionsProvider 的 ContentProvider。这是一个简单的类:

    public class MySuggestionProvider extends SearchRecentSuggestionsProvider {
      public final static String AUTHORITY = "com.example.MySuggestionProvider";
      public final static int MODE = DATABASE_MODE_QUERIES;
    
      public MySuggestionProvider() {
          setupSuggestions(AUTHORITY, MODE);
      }
    }
    

    记得将您的提供者添加到清单中,并更新您的 searchable.xml 文件,以便它知道您的提供者:

    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
      android:label="@string/app_label"
      android:hint="@string/search_hint"
      android:searchSuggestAuthority="com.example.MySuggestionProvider"
      android:searchSuggestSelection=" ?" >
    </searchable>
    

    您还需要将搜索保存在可搜索的活动中:

    if (Intent.ACTION_SEARCH.equals(Intent .getAction())) {
      String query = Intent .getStringExtra(SearchManager.QUERY);
      SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
          MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
      suggestions.saveRecentQuery(query, null);
    }
    

    【讨论】:

    • 嗨,感谢您的回复,我已经尝试过了,我得到的问题是,我的正常建议本身不起作用。也就是说,现在使用快速搜索搜索我的数据库对我来说非常有用,当我点击搜索按钮时,只有最近的建议不会出现。现在我有一个扩展 ContentProvider 的提供程序。我是否需要更改相同的提供程序,或者我应该使用扩展 SearchRecentSuggestionsProvider 的单独提供程序。非常感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 2015-03-20
    • 2011-10-23
    • 2015-01-06
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    相关资源
    最近更新 更多