开发者

Search history in Android quick search

开发者 https://www.devze.com 2023-01-25 21:41 出处:网络
I am customizing quick search to display data from my app. Its working fine. Now the i开发者_运维百科ssue is, When I click on search button, I am not able to see the search history. What should I do g

I am customizing quick search to display data from my app. Its working fine. Now the i开发者_运维百科ssue is, When I click on search button, I am not able to see the search history. What should I do get the search history (previously searched keywords)?


If you go through the tutorial on developer.android.com, I think you will find what you are looking for:

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

The trick is to implement a ContentProvider which extends SearchRecentSuggestionsProvider. It's a simple class:

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

Remember to add your provider to the manifest, and update your searchable.xml file so that it knows of your provider:

<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>

Also you will need to save the searches in your searchable activity:

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

精彩评论

暂无评论...
验证码 换一张
取 消