Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : how to execute AsyncTask?

I have to load XML data in my app, I'm doing that in a subclass of my activity class extending AsyncTask like this :

public class MyActivity extends Activity {

    ArrayList<Offre> listOffres;

    private class DownloadXML extends AsyncTask<Void, Void,Void>
    {
        protected Void doInBackground(Void... params)
        {
            listOffres = ContainerData.getFeeds();
            return null;
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_liste_offres);

        DownloadXML.execute(); // Problem here !

        for(Offre offre : listOffres) { // etc }
    }
}

I don't know how to use execute() here, I have the following error :

Cannot make a static reference to the non-static method execute(Integer...) from the type AsyncTask

I guess some parameters but what ?

Thank you.

like image 402
Rob Avatar asked Jul 12 '12 12:07

Rob


2 Answers

You need to create an instance of your DonwloadXML file and call execute() on that method:

DownloadXML task=new DownloadXML();
task.execute();

EDIT: you should probably also return the listOffers from your doInBackground() and process the array in the onPostExecute() method of your AsynTask. You can have a look at this simple AsyncTask tutorial.

like image 92
Ovidiu Latcu Avatar answered Sep 21 '22 03:09

Ovidiu Latcu


you should call it like:

new DownloadXML().execute();
like image 25
Nermeen Avatar answered Sep 21 '22 03:09

Nermeen