从网络资源中搜索建议到快速搜索框 [英] Search suggestions from network resource into Quick Search box

查看:19
本文介绍了从网络资源中搜索建议到快速搜索框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在应用程序中构建搜索,需要有一种方法将我从服务器作为 JSON 数组获取的建议放入显示在快速搜索框下方的建议列表中.

I'm building the search in an application and need to have a way of putting the suggestions which I get from my server as a JSON-array into the list of suggestions which is displayed below the Quick Search Box.

是否有一种简单的方法可以让快速搜索框读取此类资源?

Is there an easy way to have the quick search box read such resources?

目前我正在尝试使用 ContentProvider,但接口方法清楚地表明应该查询数据库以获取建议.如果您要搜索存储在应用程序中的数据,我想使用 ContentProvider 是正确的方法.但是,如果您需要查询网络资源,我不太确定这是正确的方法.

Currently I am trying to use a ContentProvider, but the interface methods clearly indicate one is supposed to be querying a database to get the suggestions. I guess using a ContentProvider is the correct way if you are searching the data which is stored inside the application. I am not so sure however, that it is the right way if you need to query a network resource.

将我从网络获得的建议保存到本地数据库对我来说毫无意义,因为建议及其命中率会不时变化.

It makes no sense for me to save the suggestions I get from the network to a local database, as the suggestions and their hit rate will vary from time to time.

有人遇到过这个问题吗?或者可以指出我类似问题的方向?我在堆栈上找不到提到网络建议的问题.

Anyone had this issue? Or can point me in the direction of a similar question? I could not find questions here on stack that mentioned network suggestions.

推荐答案

在 developer.android.com 上找到了解决方案:

Found the solution on developer.android.com:

如果您有从网络位置获得的建议,则可以在从服务器获得结果时即时构建光标.

If you have suggestions that you get from a network location, you can build a cursor on the fly when you get the results from your server.

这在 ContentProvider 的 query() 方法中:

This goes inside your ContentProvider's query() method:

String[] columns = {
   BaseColumns._ID, 
   SearchManager.SUGGEST_COLUMN_TEXT_1, 
   SearchManager.SUGGEST_COLUMN_INTENT_DATA
};

MatrixCursor cursor = new MatrixCursor(columns);

for (int i = 0; i < arr.length(); i++)
{
  String[] tmp = {Integer.toString(i), arr.getString(i), arr.getString(i)};
  cursor.addRow(tmp);
}
return cursor;

光标用于快速搜索框中以填充建议列表.

The cursor is the used in the quick-search box to fill a list of suggestions.

这篇关于从网络资源中搜索建议到快速搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆