具有动态适配器更改的 MultiAutoCompleteTextView

发布于 2024-11-02 17:11:12 字数 561 浏览 6 评论 0原文

我的 Activity 中有一个 MultiAutoCompleteTextView 小部件,其中有一个由基于 Web 的调用的结果填充的 ArrayAdapter。当用户在文本视图中输入字符时,该适配器的列表应该在后台更新。实现这一点的最佳方法是什么?

我已经尝试使用 AsyncTask 在后台下载字符串列表,但是 notifyDataSetChanged() 是从“非原始线程”调用的。而且,这似乎有点迂回。

我遇到的另一个选项是可以使用 Filterable ,但我还没有遇到任何简单的示例(AutoComplete4 似乎有点矫枉过正)如何做到这一点。如果没有示例,有人可以对我需要的演员进行广泛的概述 - 过滤器、可过滤器等。

这也是一个好方法吗?

谢谢,
拉贾特

I have a MultiAutoCompleteTextView widget in my Activity, which has an ArrayAdapter<String> that is populated by a result from a web-based call. As the user types in characters in the textview, this adapter's list should get updated in the background. What is the best way to implement this?

I have already tried to use AsyncTask to download the strings list in the background, but notifyDataSetChanged() was being called from the "non-originating thread". Moreover, this seems a little roundabout.

The other option I came across is that Filterable can be used, but I haven't come across any simple examples (AutoComplete4 seems like an overkill) on how to do this. If there are no examples, can someone give a broad overview of the actors i will need - Filter, Filterable, etc.

Also is this a good way to go?

Thanks,
Rajath

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

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

发布评论

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

评论(1

千柳 2024-11-09 17:11:12

我知道这个问题很老了,但我必须做同样的事情,我想我会与你分享解决方案,或者任何人都需要它。

首先,您确实需要使用 AsyncTask 来检索数据。话虽这么说,我永远不会让 AsyncTask 以任何方式操纵我的视图类。相反,我宁愿使用与 AsyncTask 中所需的参数一起传递的回调。 AsyncTask 完成后,您将调用回调方法,该方法将负责调用 notifyDataSetChanged()

这是一些代码:

MyAsyncTask.java

public class MyAsyncTask extends AsyncTask<Object, Void, Object> {

@Override
protected String doInBackground(Object... params) {
    MyController callbackClass = (MyController) params[0];

    // Get your other parameters and do your stuff here...

    // Call the setter with the data you get back and refresh the view
    // either here or implementing onPostExecute.
    callbackClass.setMyData(myData);
    callbackClass.refreshView();

    // Return any object if you need.
    return null;
 }
}

MyCallbackClass.java

 public MyCallbackClass extends Fragment{

   private List<MyDataType> myData;

   private ArrayAdapter<MyDataType> myAdapter;

   // Your methods including setters and getters.

    public void refreshFriendList(){
     if(myAdapter == null){
      initAutoCompleteView();
     }
     myAdapter.clear();
     myAdapter.addAll(myData);
     myAdapter.notifyDataSetChanged();
   }

}

I know this question is old, but I had to do the same thing and I figured I'd share the solution with you or anybody would need it.

First of all, you will indeed need to use an AsyncTask to retrieve your data. This being said, I'd never let an AsyncTask manipulate in any way my view class. Instead, I'd rather use a callback passed along with the parameters you need in your AsyncTask. As soon as the AsyncTask finishes, you will call your callback method which will be responsible of calling notifyDataSetChanged().

Here's some code:

MyAsyncTask.java

public class MyAsyncTask extends AsyncTask<Object, Void, Object> {

@Override
protected String doInBackground(Object... params) {
    MyController callbackClass = (MyController) params[0];

    // Get your other parameters and do your stuff here...

    // Call the setter with the data you get back and refresh the view
    // either here or implementing onPostExecute.
    callbackClass.setMyData(myData);
    callbackClass.refreshView();

    // Return any object if you need.
    return null;
 }
}

MyCallbackClass.java

 public MyCallbackClass extends Fragment{

   private List<MyDataType> myData;

   private ArrayAdapter<MyDataType> myAdapter;

   // Your methods including setters and getters.

    public void refreshFriendList(){
     if(myAdapter == null){
      initAutoCompleteView();
     }
     myAdapter.clear();
     myAdapter.addAll(myData);
     myAdapter.notifyDataSetChanged();
   }

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