1

I had a activity which contains one button and a listview.The listview contains values from webservice.When i clicked on this listview item,it shows the details in another activity.

So the thing is, can we use 4 asynctask inner class in a single activity. ie one for initial loading of contents in listview,Second will call when i click each list item,and third one for when i move to next page in listview(Listview shows only 10 items/page) and fourth will call when i clicked the button.

Is that the right method?

KP_
  • 1,854
  • 25
  • 43
  • Do you need to perform heavy tasks in each of them? Because if you don't fetch data from a webservice in all of them, I would reduce them. Try to cache everything in the first AsyncTask and just load the data when you navigate through your app – WarrenFaith Jan 03 '12 at 12:31
  • all the 4 uses different webservice calls. – KP_ Jan 03 '12 at 12:34
  • Then I think it is ok to have four tasks. They should be only inner class when you are accessing them only from your activity. – WarrenFaith Jan 03 '12 at 12:39
  • .Ok Warren.Thanks for your fast reply.Let me check it. – KP_ Jan 03 '12 at 12:41

1 Answers1

3

Yes , its right . U can as many times as u want to call . But a small modification can be done , instend of call 4 AsyncTask , u can one AsyncTask by making its generic in flow .
`

public class GetDataFromNetwork extends AsyncTask<Void,String,Object> {private  String ipAddress;
private  String webService;
private  String nameSpace;
private  String methodName;
private Context context;
private ProgressDialog progressDialog;
private SoapObject request;
private GetDataCallBack callBack;


/**
 * Set default progress Bar properties. Internal function called from setConfig 
 *   
 * @param progressBarStyle : Progress Dialog style (spinner or horizontal)
 * @param message : Message to be displayed on progressBar
 */
protected void setProgresDialogProperties(int progressBarStyle,
        String message) {
    progressDialog = new ProgressDialog(context);
    progressDialog.setProgress(0);
    progressDialog.setMax(100);
    progressDialog.setProgressStyle(progressBarStyle);
    progressDialog.setMessage(message);
}


public GetDataFromNetwork(Context context,int progressBarStyle,String message ,GetDataCallBack callBack) {
    this.callBack=callBack;
    this.context=context;
    setProgresDialogProperties(progressBarStyle, message);
}

/**
 * Used for setting up the location and config of remote location/webservice 
 * @param ipAddress : IP of webservice
 * @param webService : Name of webservice
 * @param nameSpace : NameSpace of webService
 * @param methodName :Name of function of the particular webservice
 */
public void setConfig(String ipAddress,String webService,String nameSpace,String methodName){
    this.ipAddress=ipAddress;
    this.webService=webService;
    this.nameSpace=nameSpace;
    this.methodName=methodName;

}


/**
 * Set parameter in SoapObject
 * @param requestParamater : Map of request
 */
@SuppressWarnings("rawtypes")
public void sendData(HashMap<String, Object> requestParamater) {
    }
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog.show();
}
@Override
protected Object doInBackground(Void... params) {
    return result;
}
    @Override
protected void onPostExecute(Object result) {
    super.onPostExecute(result);
    progressDialog.dismiss();
}` 

Now u can call this class when ever u want to interact with ur server.

Code_Life
  • 5,742
  • 4
  • 29
  • 49