Android的:如何从网络上获取搜索建议异步? [英] Android: How to get search suggestions asynchronously from the web?

查看:218
本文介绍了Android的:如何从网络上获取搜索建议异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个可搜索的活动。现在,我想补充一点,从Web服务所采取的搜索建议。我想这些建议异步的。据添加自定义建议我需要重写的查询方法,做我的建议搜索,建立自己的 MatrixCursor 并返回。但是这就是问题所在,我要求得到的建议是asynchronically之一。因此,当结果回来从查询方法的范围,其净出方。

I have created a searchable activity. Now, i want to add search suggestions that are taken from web service. I want to get those suggestions asynchronously. According to Adding Custom Suggestions I need to override the query method, do my suggestion search, build my own MatrixCursor and return it. but this is the problem, my request for getting the suggestion is an asynchronically one. so when result is back from net it out side of query method's scope.

推荐答案

看来这个要求的建议内容提供者未在UI线程上运行,反正,根据这样的回答:http://stackoverflow.com/a/12895381/621690 。 如果你可以改变你的HTTP请求,你可以简单地把它挡住了查询方法中。可能有助于(自定义的可能)监听中断或其他信号,以阻止不必要的请求。

It seems that the request to the suggestion content provider is not run on the UI thread, anyway, according to this answer: http://stackoverflow.com/a/12895381/621690 . If you can change your http request you could simply call it blocking inside the query method. Might help to listen for interruptions or other signals (custom ones maybe) to stop unnecessary requests.

另一种选择 - 如果你不想改变已经是异步的(比如,如果你使用的是Robospice)的任何请求类 - 应该只返回MatrixCursor参考,并填充它以后。该AbstractCursor类已经实现了观察者模式,并发出通知的情况下更改的。如果搜索系统正在侦听它应该处理的数据进行任何修改。我还没有实现我自己,所以我不能确定它会制定出的很好,因为我想象它。 (看一看CursorLoader的来源更多的灵感。)

Another option - if you do not want to change any request classes that are already asynchronous (like if you are using Robospice) - should be to just return the MatrixCursor reference and populate it later on. The AbstractCursor class already implements the Observer pattern and sends out notifications in case of changes. If the search system is listening it should handle any changes in the data. I have yet to implement that myself so I cannot confirm that it will work out as nicely as I picture it. (Have a look at CursorLoader's source for more inspiration.)

和,反正不是一个光标,整个点?否则,我们可以简单地返回数据的列表。

And, anyway, isn't that the whole point of a cursor? Otherwise we could simply return a list with data.

更新: 对我来说,使用MatrixCursor没有发挥出来。相反,我已经实现了其他两种解决方案:

UPDATE: For me, using a MatrixCursor didn't work out. Instead, I have implemented two other solutions:

  1. 使用相结合的AutoCompleteTextField与ArrayAdapter的自定义子类,它内部使用过滤器的自定义子类。该方法过滤#performFiltering()(我重写与远程服务同步调用),异步调用和UI线程没有被阻塞。
  2. 使用带有SearchableActivity和一个自定义ArrayAdapter(不含自定义过滤器)的SearchWidget。当搜索意图进来,远程请求启动(Robospice),当它通过回调回来,我请下面的自定义方法我 ArrayAdapter<标签> 子类:

  1. Using the AutoCompleteTextField in combination with a custom subclass of ArrayAdapter which itself uses a custom subclass of Filter. The method Filter#performFiltering() (which I override with the synchronous call to the remote service) is called asynchronously and the UI thread is not blocked.
  2. Using the SearchWidget with a SearchableActivity and a custom ArrayAdapter (without custom Filter). When the search intent comes in, the remote request is started (Robospice) and when it comes back via callback, I call the following custom method on my ArrayAdapter<Tag> subclass:

public void addTags(List<Tag> items) {
    if (items != null && items.size() > 0) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            super.setNotifyOnChange(false);
            for (Tag tag : items) {
                super.add(tag);
            }
            super.notifyDataSetChanged();
        } else {
            super.addAll(items);
        }
    }
}

此方法需要触发通知的适配器,从而在搜索结果列表中的服务。

This method takes care of triggering the notifications on the adapter and thus the search result list.

这篇关于Android的:如何从网络上获取搜索建议异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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