如何在上次执行完成后执行相同的AsyncTask次数

时间:2015-08-04 09:19:17

标签: android android-asynctask

我想在一系列中执行AsyncTask作为继续过程。当你看到代码时,我想调用AsyncTask(小于ArryList的大小)次。但是根据逻辑,我需要遵循一个条件,我需要在调用下一次执行之前等待先前的执行完成,否则在下次调用时,执行可能会引用先前的数据并将其存储到server.So我该如何实现呢?

for(int i=0;i<productsListcart.size();i++)//productsListcart is ArrayList<HashMap<String, String>> object,containing data.
{
    HashMap<String, String> success=productsListcart.get(i);
    product_img=success.get(TAG_PRODUCT_IMAGE);
    product_n=success.get(TAG_PRODUCT_NAME);
    product_p=success.get(TAG_PRODUCT_PRICE);
    product_q=success.get(TAG_PRODUCT_QUANTITY);
    new InsertSlipProducts().execute();//AsyncTask calling
}

1 个答案:

答案 0 :(得分:0)

试试这个, 一个粗略写的片段, 只考虑逻辑

int i=0;
if(i<productsListcart.size()){
    HashMap<String, String> success=productsListcart.get(i);
    product_img=success.get(TAG_PRODUCT_IMAGE);
    product_n=success.get(TAG_PRODUCT_NAME);
    product_p=success.get(TAG_PRODUCT_PRICE);
    product_q=success.get(TAG_PRODUCT_QUANTITY);
    new InsertSlipProducts().execute();//AsyncTask calling
    i++;
}


class InsertSlipProducts extends AsyncTask<..>{
/*execute next async task after the current is complete*/
   void onPostExecute(){
       if(i<productsListcart.size()){            
            HashMap<String, String> success=productsListcart.get(i);
            product_img=success.get(TAG_PRODUCT_IMAGE);
            product_n=success.get(TAG_PRODUCT_NAME);
            product_p=success.get(TAG_PRODUCT_PRICE);
            product_q=success.get(TAG_PRODUCT_QUANTITY);
            new InsertSlipProducts().execute();//AsyncTask calling
            i++;
       }
  } 
}