Android系统搜索框架实战:提示最近查询内容

2016-03-16 16:23 阅读 10,065 次 评论 7 条
目录:

  • 摘要
  • 基础介绍
  • 创建一个Content Provider
  • 修改xml配置文件
  • 存储每次查询内容
  • 清除最近查询记录

摘要

当你使用search dialog或search widget,你需要根据最近查询输入提示用户查询内容,例如:如果你之前查询了“apple”这个词,当你点击查询输入框时会提示下拉列表,如图1所示:
001-display recent query suggestions

基础介绍

最近的查询提示是简单的保留了查询的内容,当用户选中了其中的一个提示时,你的searchable activity到一个携带了查询内容的ACTION_SEARCH intent,提示最近的查询内容,你需要完成的几个步骤:

  • 实现一个searchable activity,正如上文所说的《如何创建一个搜索接口》
  • 继承SearchRecentSuggestionsProvider创建一个content provider,并在manifest中声明
  • 修改xml配置文件,添加content provider的信息
  • 将每次执行查询的内容保存在content provider中

当系统确认你的activity是一个searchable activity并提供查询提示时,下面的过程在用户输入时发生:

  • 系统获取查询输入内容,然后查询content provider包含在查询提示列表中
  • content provider返回一个Cursor罗列出所有匹配查询内容的查询提示
  • 系统展示由Cursor提供的查询提示内容

一旦近期的查询建议显示,以下可能会发生:

  • 如果用户键入另一个键,或以任何方式改变了查询时,重复上述步骤,并提示列表被更新。
  • 如果用户执行了查询操作,提示列表被忽略同时查询内容被以ACTION_SEARCH intent发送到searchable activity
  • 如果用户选择了其中一个查询提示内容,ACTION_SEARCHintent 使用提示内容发送到searchable activity

创建一个Content Provider

你存储最近查询内容提示的content provider必须是SearchRecentSuggestionsProvider的实现类,实际上该类为你处理所有的事情,你只需要在构造方法中添加一行代码,代码如下:

  1. public class MySuggestionProvider extends SearchRecentSuggestionsProvider {  
  2.     public final static String AUTHORITY = "com.example.MySuggestionProvider";  
  3.     public final static int MODE = DATABASE_MODE_QUERIES;  
  4.   
  5.     public MySuggestionProvider() {  
  6.         setupSuggestions(AUTHORITY, MODE);  
  7.     }  
  8. }  

setupSuggestions()方法传入search authority和database mode。search authority是一个唯一的字符串,最好是用一个高质量的名字为content provider命名(通常使用:包名+类名形式),database mode必须包含DATABASE_MODE_QUERIES,同时也可以包含DATABASE_MODE_2LINKS,该模式添加多一列到提示列表,例如:

  1. public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;  

其次,你需要在manifest中使用同一个search authority字符串声明content provider,代码如下:

  1. <application>  
  2.     <provider android:name=".MySuggestionProvider"  
  3.               android:authorities="com.example.MySuggestionProvider" />  
  4.     ...  
  5. </application>  

修改xml配置文件

配置系统使用提示内容,你需要添加android:searchableSuggestAuthorityandroid:searchSuggestSelection属性到xml的配置文件中,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <searchable xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:label="@string/app_label"  
  4.     android:hint="@string/search_hint"  
  5.     android:searchSuggestAuthority="com.example.MySuggestionProvider"  
  6.     android:searchSuggestSelection=" ?" >  
  7. </searchable>  

存储每次查询内容

为了展示每次查询的内容,输入的查询会被searchable activity放入SearchRecentSuggestionProvider中。首先,你需要实例化SearchRecentSuggestions并在每次searchable activity接收查询内容时调用saveRecentQuery(),代码如下:

  1. @Override  
  2. public void onCreate(Bundle savedInstanceState) {  
  3.     super.onCreate(savedInstanceState);  
  4.     setContentView(R.layout.main);  
  5.   
  6.     Intent intent  = getIntent();  
  7.   
  8.     if (Intent.ACTION_SEARCH.equals(intent.getAction())) {  
  9.         String query = intent.getStringExtra(SearchManager.QUERY);  
  10.         SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,  
  11.                 MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);  
  12.         suggestions.saveRecentQuery(query, null);  
  13.     }  
  14. }  

上面中的SearchRecentSuggestionsProvider构造方法需要使用相同的search authority和database mode。saveRecentQuery()方法拿去查询的字符串作为第一个参数,可选的另一个字符串包含在提示列表的另一行中,通常第二个参数必须是在你设置database mode为DATABASE_MODE_2LINKS之后才会显示,效果如图2
002-second parameter

清除最近查询记录

为了保证用户的隐私,需要在提示查询内容后给用户提供清除查询记录的功能,调用SearchRecentSuggestProvider中的clearHistory()方法,代码如下:

  1. SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,  
  2.         HelloSuggestionProvider.AUTHORITY, HelloSuggestionProvider.MODE);  
  3. suggestions.clearHistory();  

PS:源码Demo集合在《Android系统搜索框架实战:创建第一个搜索接口》文章中

你可能感兴趣的文章

来源:TeachCourse每周一次,深入学习Android教程,关注(QQ158#9359$239或公众号TeachCourse)
转载请注明出处: https://www.teachcourse.cn/1577.html ,谢谢支持!

资源分享

Android开发之枚举(Enum)在实际项目中的应用 Android开发之枚举(Enum)在实
Android面试笔记四:航天精一 Android面试笔记四:航天精一
Eclipse关联android-support-v4包的两种方法 Eclipse关联android-support-v4
一篇文章,学完TeachCourse2016成长笔记 一篇文章,学完TeachCourse201