TestSubject
TestSubject

Reputation: 3

AsyncTask won't let me use my own class

I have an AsyncTask to load entries in a ListView by creating everytime a new instance of a class, where the varaibles will be saved and let them load from an own Adapter which is extended as a BaseAdapter.

The class looks like this:

public class PictureResults
{
    private long id;
    private String pictureURL;

    public void setId(long id)
    {
        this.id = id;
    }

    public long getId()
    {
        return id;
    }

    public void setPictureURL(String pictureURL)
    {
        this.pictureURL = pictureURL;
    }

    public String getPictureURL()
    {
        return pictureURL;
    }
}

This is my AsyncTask class:

private class getPictureDataTask extends AsyncTask<Long, String, Void>
    {
        protected PictureResults doInBackground(Long... param)
        {
                PictureResults pr;

                pr = new PictureResults();
                pr.setId(param[0]);
                pr.setPictureURL("http://www.myurl.com");

                return pr;
        }

        protected void onProgressUpdate(String... response)
        {
            //do nothing
        }

        protected void onPostExecute(PictureResults ret)
        {
            picResults.add(ret); //picResults is an ArrayList<PictureResults>
            ret.loadPicture(customAdapter);
            customAdapter.notifyDataSetChanged();
        }
    }

The main problem which appears is that Eclipse let me know the following:

The return type is incompatible with AsyncTask<Long,String,Void>.doInBackground(Long[])

It is important that especially the customAdapter.notifyDataSetChanged(); will be executed in onPostExecute because in onProgressUpdate sometimes my app crashes causing of changing the ListView from a background thread.

Why I can't use PictureResults as a type???

BTW: This is how I execute the AsyncTask:

Long hey = (long) 1;
new getPictureDataTask().execute(hey);

Upvotes: 0

Views: 184

Answers (5)

minjie
minjie

Reputation: 79

you should change your AsyncTask to AsyncTask

Upvotes: 0

Bolton
Bolton

Reputation: 2256

Take a look at the 3 parameters in the angle brackets while you implements your AsyncTask.

AsyncTask<Params, Progress, Result>  

The Params is take by the doInBackground() method which you passing it when you start the task with excute();
The Result should be return by the doInBackground() method and will be taken by the method onPostExecute(Result ret).
Each should be of the type you hava defined when you implements you custom task class.
More information is here.

Upvotes: 0

Glenn
Glenn

Reputation: 12819

The third type parameter of AsyncTask should be PictureResults not Void

class getPictureDataTask extends AsyncTask<Long, String, PictureResults>

Because the 3rd Parameter Type of AsyncTask is what kind of data result should be returned. If you don't want to return any result from doInBackground(), use Void and then return null;

Upvotes: 1

VinceStyling
VinceStyling

Reputation: 3827

when you wanted return PictureResults, you should be declare in the AsyncTask.

private class getPictureDataTask extends AsyncTask<Long, String, PictureResults>
{
    ......
}

Upvotes: 0

nedaRM
nedaRM

Reputation: 1827

You should change this to AsyncTask

Upvotes: 0

Related Questions