Keshri_raj
Keshri_raj

Reputation: 81

Run Asynctask class inside another asynctask

I have a class where I have an inner asynctask class which performs json parsing. I have called that asynctask class and sucessfully done parsing and shown in a list view. Now based on an ID(product id) in that list view I have to show images in the very same list view corresponding to that products. For that another url has to be fired and parsing has to be done. So I have stored all the product id's in a array list and based on that i am doing

for(int z=0;z<Prdtspcl_pid.size();z++){
    String s=Prdtspcl_pid.get(z).toString();
    String spclurl=url to be fired+s;   
    MySpclMethod msm2= new MySpclMethod(spclurl);
    msm2.execute();

}

where prdt_pid is my product_id,and MySpclMethod is another inner asynctask class for image parsing.

Now what should I do & where should I call the second asynctask class?

Upvotes: 0

Views: 721

Answers (1)

ddewaele
ddewaele

Reputation: 22603

You should run all your run logging tasks in this single AsyncTask. It's perfectly fine to do this as it already provides you with a background thread do to all your processing.

Just do all your http processing in the main doInBackGround method.

Whenever you want to update the UI you have 2 hooks:

  • onPostExecute : This gets called when the doInBackground completes. Put all logic here that you want to perform on the UI when your background processing is done completely.
  • onProgressUpdate : This gets called whenever you call publishProgress from within the doInBackground method. Do this whenever you want to update the UI while your background processing is still ongoing. It allows you to show intermediate progress. This will cause the AsyncTasks onProgressUpdate to be called automatically where you can update your UI.

If the adapter used to back your ListView is a member variable of the activity class then you can reference it from onPostExecute and onProgressUpdate meaning you'll be able to update your list either when

  • the background processing has completed (onPostExecute)
  • when background processing is still ongoing (onProgressUpdate)

An example is shown here : http://developer.android.com/reference/android/os/AsyncTask.html

Nested AsynTasks are not supported (and could never work).

Upvotes: 3

Related Questions