即使更新 ArrayAdapter,AutoCompleteTextView 也不显示结果

发布于 2024-08-29 07:32:30 字数 468 浏览 4 评论 0原文

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

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

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

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

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哥,最终变帅啦 2024-09-05 07:32:30

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

我认为您可能误解了 AsyncTask 的需要。 Filter 对象已经在执行与 AsyncTask 类似的操作:performFiltering() 在后台线程中完成,并且在 PerformFiltering() 之后从 UI 线程调用 publishResults()完成了。因此,您可以直接在performFiltering()中执行网络请求,并将结果设置到FilterResults对象中,并且您不必担心网络请求太慢而导致UI出现问题。

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

// 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() 中获取同步结果,则上面的代码示例只是一种可能性。

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.

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.

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();
    }
}

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().

茶花眉 2024-09-05 07:32:30

我认为乔的回答是正确的选择。但是,我认为您应该使用 CountDownLatch 而不是等待/通知。

原因是,使用 wait/notify,如果您的 API 在启动“wait()”之前实际上返回速度非常快,那么您将面临竞争条件的风险……在这种情况下,notify 不会产生任何效果,而 wait() 将无限期地等待。
使用 Latch,代码将如下所示(从 Joe 复制并修改):

// 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;
  CountDownLatch latch;

  synchronized APIResult waitForAPI(CharSequence constraint) {
      latch = new CountDownLatch(1);
      someAPIObject.startAsyncNetworkRequest(constraint);
      // At this point, control returns here, and the network request is in-progress in a different thread.
      try {
        // Will wait till the count is 0...
        // If the count is already 0, it'll return immediately. 
        latch.await();
        // 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.
    // countDown() will wake up the other calling thread, allowing it to continue
    // execution in the performFiltering() method, as usual.
    latch.countDown();
  }
}

最后,我没有足够的信用来发表评论,否则我会......

I think Joe's answer is the way to go. However, I think you should use CountDownLatch instead of wait/notify.

The reason is, with wait/notify, you risk a race condition if your API actually return super fast before you start "wait()"... in this case, notify won't have an effect and wait() will wait indefinitely.
With Latch, the code will look like this (copied from Joe and modified):

// 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;
  CountDownLatch latch;

  synchronized APIResult waitForAPI(CharSequence constraint) {
      latch = new CountDownLatch(1);
      someAPIObject.startAsyncNetworkRequest(constraint);
      // At this point, control returns here, and the network request is in-progress in a different thread.
      try {
        // Will wait till the count is 0...
        // If the count is already 0, it'll return immediately. 
        latch.await();
        // 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.
    // countDown() will wake up the other calling thread, allowing it to continue
    // execution in the performFiltering() method, as usual.
    latch.countDown();
  }
}

Lastly, I don't have enough credit to post a comment, otherwise I would have...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文