即使更新了 ArrayAdapter,AutoCompleteTextView 也不显示结果 [英] AutoCompleteTextView not displaying result even when the ArrayAdapter is updated

查看:32
本文介绍了即使更新了 ArrayAdapter,AutoCompleteTextView 也不显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AutoCompleteTextView(ACTV) 来显示我从网络资源获取的结果.我已经将完成阈值设置为 2,我可以看到当我输入字符时请求被触发.

I'm trying to get an AutoCompleteTextView(ACTV) to display results I'm a getting from a network resource. I have set the completion-treshold to 2 and I can see that the request is fired when I enter to characters.

我得到的结果是正确的.假设我写了ca",我得到了结果car"作为自动完成.我有一个回调函数,它接收来自 AsyncTask 的结果并将结果放入 ArrayAdapter.然后我在 ACTV 上调用 .showDropDown() 并显示一个空的下拉列表(正常元素大小的一半).然后如果我输入最后一个字母r"并且ACTV显示car",则显示下拉列表,结果突然出现在列表中.

The result I am getting is the correct one. Lets say I write "ca", and I get the result "car" as an autocompletion. I have a callback function which receives the result from an AsyncTask and puts the result into the ArrayAdapter. Then I call .showDropDown() on the ACTV and an empty dropdown is shown (half the size of a normal element). Then if I enter the last letter "r" and the ACTV shows "car", the dropdown is shown and the result is suddenly in the list.

如果我输入了两个字符(返回有效结果),然后删除最后一个字母,也会发生同样的情况.删除字母后,car"将显示为自动完成值.

The same happens if I have entered two characters (which returns a valid result), and the remove the last letter. When the letter is removed, "car" is shown as an autocompletion value.

有人遇到过这个问题吗?看起来适配器中充满了结果,但直到我执行下一个操作时才会显示结果.在将结果添加到适配器后,我还尝试运行 .notifyDataSetChanged(),但这应该不需要,或者?

Has anyone had this problem? It looks like the adapter is filled with the result, but the result does not show until the next action I do. I have also tried to run .notifyDataSetChanged() after I have added the result to the adapter, but that should not be needed, or?

推荐答案

如果没有看到您的代码,就很难判断会发生什么.但首先想到的是您的网络请求发生在不同的线程上,因此您的 performFiltering() 可能会过早地返回一个空的结果集.此时, publishResults() 返回空结果,而您的下拉列表为空.稍后,您的 AsyncTask 将返回其结果,并将结果添加到适配器的列表中,但由于某种原因,它尚未显示.

Without seeing your code, it's hard to tell what could be going on. But the first thing that comes to mind is that your network request is happening on a different thread, and therefore your performFiltering() may be returning an empty result set prematurely. At that point, publishResults() is returning the empty result, and your dropdown is empty. Later, your AsyncTask will get its result back, and you add the results into the adapter's list, but for one reason or another, it doesn't get displayed yet.

不过,我认为您可能误解了需要 AsyncTask.Filter 对象已经在做类似于 AsyncTask 的事情:performFiltering() 在后台线程中完成,并且 publishResults() 在 performFiltering() 之后从 UI 线程调用完成了.这样你就可以直接在 performFiltering() 中做你的网络请求,并将结果设置到 FilterResults 对象中,你就不用担心网络请求太慢而导致你的 UI 出现问题.

I think you may be mistaken about the need for AsyncTask though. The Filter object is already doing something similar to AsyncTask: performFiltering() is done in a background thread, and publishResults() is called from the UI thread, after performFiltering() is finished. So you can do your network request directly in performFiltering(), and set the results into the FilterResults object, and you won't have to worry about the network request being too slow and causing problems in your UI.

另一种解决方案,稍微复杂一些,但这就是我在我的 Filter 对象中所做的(由于现有架构在后台执行 API 调用,根据需要使用异步回调而不是阻塞/同步步骤对于performFiltering()),就是用一个带有wait()/notify()的同步对象做跨线程监听,所以效果和在performFiltering()中直接做网络请求是一样的,但实际上是发生在多个线程:

An alternative solution, which is slightly more complicated, but it's what I'm doing in my Filter object (due to existing architecture that does API calls in the background, using an asynchronous callback instead of the blocking/synchronous step as required for performFiltering()), is to use a synchronized object with wait()/notify() for doing cross-thread monitoring, so the effect is the same as doing the network request directly in performFiltering(), but it's actually happening in multiple threads:

// in Filter class..
protected FilterResults performFiltering(CharSequence constraint) {

    APIResult response = synchronizer.waitForAPI(constraint);
    // ...
}

// callback invoked after the API call finishes:
public void onAPIComplete(APIResult results) {
    synchronizer.notifyAPIDone(results);
}

private class Synchronizer {
    APIResult result;

    synchronized APIResult waitForAPI(CharSequence constraint) {
        someAPIObject.startAsyncNetworkRequest(constraint);
        // At this point, control returns here, and the network request is in-progress in a different thread.
        try {
            // wait() is a Java IPC technique that will block execution until another
            // thread calls the same object's notify() method.
            wait();
            // When we get here, we know that someone else has just called notify()
            // on this object, and therefore this.result should be set.
        } catch(InterruptedException e) { }
        return this.result;
    }

    synchronized void notifyAPIDone(APIResult result) {
        this.result = result;
        // API result is received on a different thread, via the API callback.
        // notify() will wake up the other calling thread, allowing it to continue
        // execution in the performFiltering() method, as usual.
        notify();
    }
}

但是,我认为您可能会发现最简单的解决方案是直接在 performFiltering() 方法中同步执行您的网络请求.上面的代码示例只是一种可能性,如果您已经拥有用于异步/回调驱动的 API 调用的架构,并且您不想更改该行为以在 performFiltering() 中获得同步结果.

However, I think you may find that the easiest solution is to just do your network request synchronously, directly in the performFiltering() method. The above code example is just one possibility, if you already have the architecture in place for asynchronous/callback-driven API calls, and you don't want to change that behavior in order to get synchronous results in performFiltering().

这篇关于即使更新了 ArrayAdapter,AutoCompleteTextView 也不显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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