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

查看:197
本文介绍了即使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显示汽车,则会显示下拉列表,并且结果突然出现在列表中。

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.

如果我输入了两个字符(返回有效结果),然后删除最后一个字母。删除字母后,汽车显示为自动完成值。

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()中发出网络请求,并将结果设置到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进行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天全站免登陆