0

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?

Aditya
  • 5,509
  • 4
  • 31
  • 51
Keshri_raj
  • 81
  • 1
  • 8

1 Answers1

3

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).

ddewaele
  • 22,363
  • 10
  • 69
  • 82
  • bt preexecute & postexecute run on uithread...so i hv kept the for loop in post method of 1st asyntask..that wrk bt for the product only..means only last product's image and description are showing.. – Keshri_raj Jul 04 '13 at 05:18
  • No ... If you for loop does any kind of network processing or other long running tasks that blocks the UI thread (and I assume it does in your case) then it needs to go in the single doInBackGround method of your single AsyncTask. – ddewaele Jul 04 '13 at 05:33
  • 1 asynctask yes. Don't know what you mean with 2processing. It all depends when and how frequent you want to update your listview. – ddewaele Jul 04 '13 at 05:39