Searchable之Recent Query Suggestions(上)

原文地址:http://developer.android.com/guide/topics/search/adding-recent-query-suggestions.html
当你使用search dialog or search widget的时候, 你可以提供基于recent search queries的provide search suggestions.
比如所用户以前搜索过"puppies," 当用户开始输入和他一样的时候,这个query将出现在suggestion中.
图一 就是一个在search dialog中使用recent query suggestions的界面图。
图一:
Recent Query Suggestions(上) - hubingforever - 民主与科学
在开始实现recent search queries之前,你需要在你的程序中实现search dialog or a search widget基本的搜索 。
关于他们的更多内存请参照《 Searchable 》系列
基本<wbr style="line-height:25px"><div style="line-height:25px">Recent query suggestions仅仅是保存搜索过的searches。当用户选择了一个suggestions的时候,你的searchable activity将收到一个把suggestion作为搜索关键字的ACTION_SEARCH intent,其实你的searchable activity曾经也处理过它。</div> <div style="line-height:25px"> <div style="line-height:25px">为了提供recent queries suggestions, 你需要以下几步:</div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px"> </span><span style="line-height:25px">A</span><span style="color:#0000ff; line-height:25px">、 实现一个searchable activity</span> </div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px"> </span><span style="line-height:25px">B</span><span style="color:#0000ff; line-height:25px">、创建一个继承于SearchRecentSuggestionsProvider的content provider类,并在你的manifest中声明它。</span> </div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px"> </span><span style="line-height:25px">C</span><span style="color:#0000ff; line-height:25px">、在searchable配置文件中配置提供search suggestions的content provider.</span> </div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px"> </span><span style="line-height:25px">D</span><span style="color:#0000ff; line-height:25px">、当每次执行搜索的时候,把你的搜索关键字保存你的content provider中</span> </div> </div> <div style="line-height:25px"><span style="color:#003366; line-height:25px">当系统显示search dialog或search widget的时候,就会在它们下面显示 search suggestions。你所需要做的就是为系统提供提取suggestions的source。</span></div> <div style="line-height:25px"> <div style="line-height:25px; color:rgb(0,51,102)">一旦系统识别到了你的searchable activity并且你提供的search suggestions, 当用户键入查询语句的时候,将执行下面的流程:</div> <div style="line-height:25px"> <span style="line-height:25px">1</span><span style="color:#0000ff; line-height:25px">.系统取得用户当前已经输入的搜索关键字,并在你的content provider(suggestions)执行查询</span> </div> <div style="line-height:25px"> <span style="line-height:25px">2</span><span style="color:#0000ff; line-height:25px">.你的content provider用Cursor返回一个和搜索关键字相匹配的所有suggestions</span> </div> <div style="line-height:25px"> <span style="line-height:25px">3</span><span style="color:#0000ff; line-height:25px">.系统以列表的形式显示由Cursor提供的suggestions</span> </div> </div> <div style="line-height:25px"> <div style="line-height:25px; color:rgb(0,51,102)">一旦recent query suggestions被显示, 下面的几种情况之一将发生:</div> <div style="line-height:25px"> <span style="line-height:25px">A</span><span style="color:#0000ff; line-height:25px">,如果用户继续键入搜索关键词,或以任何方式改变了搜索关键词,上面的行为将重复,suggestion list也将更新</span> </div> <div style="line-height:25px"> <span style="line-height:25px">B</span><span style="color:#0000ff; line-height:25px">,如果用户执行搜索的话,suggestions将被忽略,用户输入的搜索关键词将通过ACTION_SEARCH intent传递到你的searchable activity</span> </div> <div style="line-height:25px"> <span style="line-height:25px">C</span><span style="color:#0000ff; line-height:25px">,如果用户选择了一个suggestion, 将使用这个suggested text做为查询关键字从ACTION_SEARCH intent传递到你searchable activity</span> </div> <div style="line-height:25px; color:rgb(0,51,102)">你的继承自SearchRecentSuggestionsProvider的content provider将自动的做上面的工作,所以实事上你只有很少的代码需要编写。</div> </div> <div style="line-height:25px"><span style="font-size:16px; line-height:28px"><span style="line-height:28px">创建Content Provider</span></span></div> <div style="line-height:25px"> <div style="line-height:28px; font-size:16px"> <span style="color:#003366; line-height:28px">你用于recent query suggestions的content provider必须是一个SearchRecentSuggestionsProvider的实现。实际上它已经为你提供了所有的东西。</span><span style="line-height:28px; color:rgb(0,51,102)">你所需要的就是重写构造函数来加上一行代码</span> </div> <div style="line-height:28px; font-size:16px"><span style="color:#003366; line-height:28px">比如下面就是一个recent query suggestions的content provider的完整实现。</span></div> <div style="line-height:25px"><span style="line-height:25px"><span style="font-size:13px; line-height:23px">示例1</span><span style="font-size:16px; line-height:28px">:</span></span></div> <div style="line-height:28px; font-size:16px; font-weight:bold"><span style="line-height:22px; font-family:monospace; white-space:pre; font-size:14px"><span style="color:#ff6600; line-height:25px"><span style="line-height:25px">public</span><span style="line-height:25px"> </span><span style="line-height:25px">class</span><span style="line-height:25px"> </span><span style="line-height:25px">MySuggestionProvider</span><span style="line-height:25px"> </span><span style="line-height:25px">extends</span><span style="line-height:25px"> </span></span><span style="line-height:25px"><span style="color:#ff6600; line-height:25px">SearchRecentSuggestionsProvide</span><span style="color:#3366ff; line-height:25px">r</span></span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">{</span></span></div> <pre style="line-height:25px"><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">public</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">final</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">static</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">String</span><span style="line-height:25px; color:rgb(51,102,255)"> AUTHORITY </span><span style="line-height:25px; color:rgb(51,102,255)">=</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">"com.example.MySuggestionProvider"</span><span style="line-height:25px; color:rgb(51,102,255)">;</span><span style="line-height:25px; color:rgb(51,102,255)"><br style="line-height:25px"></span><span style="line-height:25px; color:rgb(51,102,255)">public</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">final</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">static</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">int</span><span style="line-height:25px; color:rgb(51,102,255)"> MODE </span><span style="line-height:25px; color:rgb(51,102,255)">=</span><span style="line-height:25px; color:rgb(51,102,255)"> DATABASE_MODE_QUERIES</span><span style="line-height:25px; color:rgb(51,102,255)">;</span><span style="line-height:25px; color:rgb(51,102,255)"><br style="line-height:25px"></span><span style="line-height:25px; color:rgb(51,102,255)">public</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">MySuggestionProvider</span><span style="line-height:25px; color:rgb(51,102,255)">()</span><span style="line-height:25px; color:rgb(51,102,255)"> </span><span style="line-height:25px; color:rgb(51,102,255)">{</span><span style="line-height:25px"><span style="color:#3366ff; line-height:25px"><br style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">setupSuggestions</span></span><span style="color:#ff6600; line-height:25px"><span style="line-height:25px">(</span><span style="line-height:25px">AUTHORITY</span><span style="line-height:25px">,</span><span style="line-height:25px"> MODE</span><span style="line-height:25px">);</span></span><span style="line-height:25px; color:rgb(51,102,255)"><br style="line-height:25px"></span><span style="line-height:25px; color:rgb(51,102,255)">}</span><span style="line-height:25px; color:rgb(51,102,255)"><br style="line-height:25px"></span><span style="line-height:25px; color:rgb(51,102,255)">}</span></pre> <pre style="line-height:25px"><span style="color:#3366ff; line-height:25px">调用</span><span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"><code style="line-height:25px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/SearchRecentSuggestionsProvider.html#setupSuggestions%28java.lang.String,%20int%29" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">setupSuggestions()</a></code>需要两个参数,一个是搜索的授权名,另一个是数据库的模式。搜索授权必须是unique的字符串,但是一般最好的就是使用你的content provider的qualified name(包名加上provider的类名; 比如 "com.example.MySuggestionProvider"). 数据库模式必须包含</span><span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"><code style="line-height:25px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/SearchRecentSuggestionsProvider.html#DATABASE_MODE_QUERIES" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">DATABASE_MODE_QUERIES</a>,也可以加上可选项</code></span><span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"><code style="line-height:25px"><a rel="nofollow" href="http://developer.android.com/reference/android/content/SearchRecentSuggestionsProvider.html#DATABASE_MODE_2LINES" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">DATABASE_MODE_2LINES</a>。</code>它将在suggestions表的基础上再加入一行,它允许你为每个suggestion再添加一行文本。比如,你想让没suggestion都有2行的话,可以这样:</span></pre> <pre style="line-height:25px"><span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"><span style="line-height:25px"><span style="line-height:25px"><span style="font-size:13px; line-height:23px">示例2:</span></span></span></span></pre> <pre style="line-height:25px"><span style="font-family:Arial,Helvetica,sans-serif; font-size:16px; color:#ff9900; line-height:28px"><span style="line-height:25px; white-space:normal">public final static int MODE = DATABASE_MODE_QUERIES | DATABASE_MODE_2LINES;</span></span></pre> <pre style="line-height:25px"><span style="font-family:Arial,Helvetica,sans-serif; font-size:13px; color:#000080; line-height:23px"><span style="line-height:25px; white-space:normal">注意你在manifest为该</span></span><span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal">content provide声明的授权必须和</span><span style="line-height:22px"><span style="color:#ff6600; line-height:22px">setupSuggestions</span></span><span style="line-height:22px"><span style="line-height:22px; color:rgb(255,102,0)">(</span><span style="line-height:22px; color:rgb(255,102,0)">AUTHORITY</span><span style="line-height:22px; color:rgb(255,102,0)">,</span><span style="line-height:22px; color:rgb(255,102,0)"> MODE</span><span style="line-height:22px"><span style="color:#ff6600; line-height:25px">);</span><span style="color:#000080; line-height:25px">中声明的授权一样。</span></span></span></pre> <pre style="line-height:25px"><span style="line-height:22px"><span style="line-height:22px"><span style="color:#000080; line-height:25px"></span></span></span><span style="line-height:22px"><span style="line-height:22px"><span style="color:#000080; line-height:25px">比如:</span></span></span><span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; white-space:normal"><span style="line-height:25px"><span style="font-size:13px; line-height:20px">示例2:</span></span></span></pre> <pre style="line-height:25px"><span style="line-height:22px"><span style="line-height:22px"></span></span><pre style="line-height:25px"><span style="line-height:25px; color:rgb(0,0,255)">&lt;application&gt;</span><span style="line-height:25px; color:rgb(0,0,255)"><br style="line-height:25px"></span><span style="line-height:25px; color:rgb(0,0,255)">&lt;provider</span><span style="line-height:25px; color:rgb(0,0,255)"> </span><span style="line-height:25px; color:rgb(0,0,255)">android:name</span><span style="line-height:25px; color:rgb(0,0,255)">=</span><span style="line-height:25px; color:rgb(0,0,255)">".MySuggestionProvider"</span><span style="line-height:25px; color:rgb(0,0,255)"><br style="line-height:25px"></span><span style="line-height:25px; color:rgb(0,0,255)">android:authorities</span><span style="line-height:25px; color:rgb(0,0,255)">=</span><span style="line-height:25px"><span style="color:#0000ff; line-height:25px">"</span><span style="color:#ff9900; line-height:25px">com.example.MySuggestionProvider</span><span style="color:#0000ff; line-height:25px">"</span></span><span style="line-height:25px; color:rgb(0,0,255)"> </span><span style="line-height:25px; color:rgb(0,0,255)">/&gt;</span><span style="line-height:25px; color:rgb(0,0,255)"><br style="line-height:25px"> ...<br style="line-height:25px"></span><span style="line-height:25px; color:rgb(0,0,255)">&lt;/application&gt;</span></pre></pre> </div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值