一个活动中有多个asynctask类

时间:2012-01-03 12:27:10

标签: android android-asynctask

我有一个活动,其中包含一个按钮和一个列表视图。列表视图包含来自webservice的值。当我点击此列表视图项时,它会在另一个活动中显示详细信息。

所以问题是,我们可以在一个活动中使用4个asynctask内部类。 即一个用于列表视图中的内容的初始加载,第二个将在我单击每个列表项时调用,第三个用于当我移动到列表视图中的下一页时(列表视图仅显示10个项目/页面),第四个将在我单击按钮时调用

这是正确的方法吗?

1 个答案:

答案 0 :(得分:3)

是的,它的权利。你可以多次打电话给你。但是可以做一个小的修改,通过调用4 AsyncTask,你可以通过在流程中制作它的泛型来实现一个AsyncTask。
`

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();
}` 

现在,如果你想与你的服务器进行交互,你可以调用这个类。