我得到的结果是正确的.让我说我写“ca”,我得到结果“汽车”作为自动完成.我有一个回调函数,它接收来自AsyncTask的结果并将结果放入ArrayAdapter.然后我在ACTV上调用.showDropDown()并显示一个空的下拉列表(正常元素大小的一半).然后,如果我输入最后一个字母“r”并且ACTV显示“car”,则显示下拉列表并且结果突然出现在列表中.
如果我输入了两个字符(返回有效结果),则删除最后一个字母也会出现同样的情况.删除该字母后,“car”显示为自动完成值.
有没有人有这个问题?看起来适配器已填充结果,但结果直到我执行的下一个操作才会显示.我已经尝试在将结果添加到适配器后运行.notifyDataSetChanged(),但是不应该这样做,或者?
解决方法
我想你可能会错误地认为需要AsyncTask. Filter对象已经在做类似于AsyncTask的事情:performFiltering()在后台线程中完成,并且在performFiltering()完成后从UI线程调用publishResults().因此,您可以直接在performFiltering()中执行网络请求,并将结果设置为FilterResults对象,您不必担心网络请求太慢并导致UI出现问题.
另一种解决方案,稍微复杂一点,但它是我在Filter对象中所做的事情(由于现有的架构在后台执行API调用,使用异步回调而不是执行过滤所需的阻塞/同步步骤( )),使用带有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();
- }
- }@H_404_17@
但是,我认为您可能会发现最简单的解决方案是直接在performFiltering()方法中同步执行您的网络请求.上面的代码示例只是一种可能性,如果您已经拥有适用于异步/回调驱动的API调用的体系结构,并且您不希望更改该行为以便在performFiltering()中获取同步结果.