-1

I have a ListView and a custom adapter that i wish to use to fill it with a String and an Image in an async task. I have to get the images from a URL and the String from a text file. This is all possible however, what i have not worked out is how to then assign the adapter to the list view as this must be done from the main thread.

Any help is appreciated.

Dan13_
  • 193
  • 1
  • 2
  • 16

5 Answers5

1

what i have not worked out is how to then assign the adapter to the list view as this must be done from the main thread.

Do that work in onPostExecute() of your AsyncTask.

How do you run an async task then use a returned variable on the main thread?

There is no "returned value" from an AsyncTask.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

use AsyncTask, start new one inside your adapters getView (or similar) method. inside doInBackground you probably know what to do, then just return to onPostExecute result, e.g. Bitmap (look out, generic!)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
1

Parse an interface to your adapter into your async task and call the interface from the onPostExecute(). Works like a charm

Tristan Richard
  • 3,385
  • 1
  • 15
  • 17
1

It's really difficult to advise when we have not seen a single line of the code you are working on. In any case, for what you asked, you have to do that in onPostExecute of your AsynTask. You add something like this:

  @Override
  protected void onPostExecute(SomeResultType results)
  {
    //your code to do stuff on UI thread goes here...
  }

Have a look at the discussions and selected answer here. I hope this helps.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
1

You can't use the variables returned in doInBackground in the Main thread. Use the variable in the onPostExecute method to do your task.