JoeHz
JoeHz

Reputation: 2196

Android ASyncTask Return type mismatch

I'm pulling my hair out with what looks to be a really simple thing to do. Surely this is just a typo or something stupid, but I'm just not seeing it...

I have an ASyncTask class defined as such (and it compiles fine)

    public class SectionRetrieval extends AsyncTask<Void, Void, List<Section>>
    {


        @Override
        protected List<Section> doInBackground(Void... voids)
        {        
            List<Section> sections = new ArrayList<Section>();

            ...

            return sections;
        }
     }

In my app I attempt to call it as such

    List<Section> sections = new SectionRetrieval().execute();

But this doesn't compile

Error:(472, 63) Gradle: error: incompatible types: 

AsyncTask<Void,Void,List<Section>> cannot be converted to List<Section>

I've tried separating the creation of the new SectionalRetrieval instance and the call the .execute(). No such luck. It really does seem to think execute() is really trying to return an instance of SectionalRetrieval.

So how did I make it think that's what I wanted?

Upvotes: 1

Views: 196

Answers (2)

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

All you need to do to run this is:

new SectionRetrieval().execute();

This method doesn't return anything. It's is an asynchronous task, thus no return value. Once the task is done, onPostExecute will run, and that is where you handle the return values, not where you call the execute method.

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191728

You receive whatever you return from doInBackground in the parameter of onPostExecute.

You update the UI from onPostExecute as well.

You could do execute().get() to have it compile, probably, but I wouldn't recommend that

Upvotes: 1

Related Questions