开发者

MultiAutoCompleteTextView with dynamic adapter changes

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'开发者_JAVA百科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


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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜