6

how do I return a List generated in an AsyncTask back to the Activity?

My LoadStringsAsync class:

public class LoadStringsAsync extends AsyncTask<Void, Void, List<String> > {

    List<String> str;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ...
    }

    @Override
    protected List<String> doInBackground(Void... arg0) {
        ...
        get content from the internet
        and
        fill the list
        ...
    }

    @Override
    protected void onPostExecute(List<String> str) {
        super.onPostExecute(events);
        ...

    }
}

and I need the List back in my Activity to work with it. (No not only to show it in ListView :P)

Any suggestions how to do this? :-)

Thanks so far!

Hsnbrg
  • 779
  • 3
  • 7
  • 14
  • http://stackoverflow.com/questions/17398837/processdialog-is-not-appearing-properly. check this in this case you return a string. modify accordingly – Raghunandan Jul 11 '13 at 14:34
  • It probably depends on what you want to do with the List. Can you provide us more information? – Enrichman Jul 11 '13 at 14:39

5 Answers5

10

your Activity:

public class YourActivity extends Activity {
    private List<String> list = new ArrayList<String>();

    public void onCreate(Bundle state) {
        //...
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    private void fireYourAsyncTask() {
        new LoadStringsAsync(this).execute();
    }

}    

AsyncTask:

public class LoadStringsAsync extends AsyncTask<Void, Void, List<String>> {

    List<String> str;
    private YourAcitivity activity;

    public LoadStringsAsync(YourAcitivity activity) {
        this.activity = activity;
    }

    @Override
    protected List<String> doInBackground(Void... arg0) {
    }

    @Override
    protected void onPostExecute(List<String> str) {
        super.onPostExecute(events);
        activity.setList(str);
    }
}
Evin1_
  • 12,292
  • 9
  • 45
  • 47
wtsang02
  • 18,603
  • 10
  • 49
  • 67
2

If you make your AsyncTask an inner class in your Activity, then onPostExecute will be in the same context as the Activity, and you can use the List as you would anywhere in the Activity.

If you don't make your AsyncTask an inner class in your Activity, then you need to set up a listener to call back to your Activity when the AsyncTask finishes. If you prefer to go that route look at this post.

There is also this post that is similar to your question.

Community
  • 1
  • 1
yiati
  • 995
  • 1
  • 12
  • 27
  • 4
    Its not called subclass. Wording should be ." Implement AsyncTask as a innerclass in your Activity". – wtsang02 Jul 11 '13 at 14:40
2

According to the documentation from android link you can use the get method to return computational result. Since your AsyncTask shows that you are returning a List, then you can try the following code in the class you are calling the asynchronous class

List<String> returnedlist=new LoadStringAsync().execute().get();

Hoping this option helps.

Tonui Nicholus
  • 395
  • 2
  • 8
0

Place code that uses the list, or sets it to a variable, inside onPostExecute().

Better run your task from parent Activity like:

public class MyActivity extends Activity{
  private List<String> mData = new ArrayList<String>();


  private void runTask(){
    new LoadStringsAsync(){

      @Override
      protected void onPostExecute(List<String> str) {
        super.onPostExecute(events);
        mData.clear();
        mData.addAll(str);
      }

    }.execute();
  }
}
S.D.
  • 29,290
  • 3
  • 79
  • 130
0

You can send your list to a handler inside a Message :

Message msg = Message.obtain();
msg.obj = myList;
handler.sendMessage(msg);

More infos : http://developer.android.com/reference/android/os/Message.html

Akram Fares
  • 1,653
  • 2
  • 17
  • 32