2010-11-18 156 views
3

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

这是非常迫切的,任何人都可以帮助我如何得到这个?

回答

2

如果你经历上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); 
} 
+0

嗨,谢谢你的回复,我已经试过了,我得到的概率是,我的正常建议本身不工作。也就是说,现在使用快速搜索来搜索我的数据库对我来说是完美的工作,只有当我点击搜索按钮时,最近的建议才会出现。现在我有一个扩展ContentProvider的提供者。我是否需要更改相同的提供者,或者我应该使用扩展SearchRecentSuggestionsProvider的独立提供者。非常感谢你的回复。 – Padma 2010-11-26 06:38:24