【发布时间】:2011-05-11 21:11:00
【问题描述】:
我正在自定义快速搜索以显示来自我的应用程序的数据。它工作正常。现在的问题是,当我点击搜索按钮时,我无法看到搜索历史。我应该怎么做才能获得搜索历史(以前搜索过的关键字)?
【问题讨论】:
标签: android history quick-search
我正在自定义快速搜索以显示来自我的应用程序的数据。它工作正常。现在的问题是,当我点击搜索按钮时,我无法看到搜索历史。我应该怎么做才能获得搜索历史(以前搜索过的关键字)?
【问题讨论】:
标签: android history quick-search
如果你浏览 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);
}
【讨论】: