2014-09-04 48 views
0

當然,我的代碼中刪除了一些不重要的部分。這裏是擴展AsyncTask的3類。 LoadAllProducts執行三個DownloadTask,每個DownloadTask執行ParserTask和ParserTask填充ArrayList。畢竟完成後,我想用arrayList去做。當填充ArrayList的asynctasks完成時,我想要使用ArrayList

public class MapActivity extends FragmentActivity { 
     private ProgressDialog pDialog; 
     DownloadTask downloadTask; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.map_layout); 
      dists = new ArrayList<String>(); 
      // Loading products in Background Thread 
      new LoadAllProducts().execute(); 
    } 

....在這裏我想與填充後的ArrayList dists中工作.....

// Fetches data from url passed 
    private class DownloadTask extends AsyncTask<String, Void, String> 
    { 
     // Downloading data in non-ui thread 
     @Override 
     protected String doInBackground(String... url){} 

     // Executes in UI thread, after the execution of doInBackground() 
     @Override 
     protected void onPostExecute(String result){ 
      super.onPostExecute(result); 
      parserTask.execute(result); 
     } 
    } 

    /** A class to parse the Google Places in JSON format */ 
    private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>>{ 
     // Parsing the data in non-ui thread 
     @Override 
     protected List<List<HashMap<String, String>>> doInBackground(String... jsonData){ 
     } 

     // Executes in UI thread, after the parsing process 
     @Override 
     protected void onPostExecute(List<List<HashMap<String, String>>> result){ 
      //here is arrayList filled 
      dists.add(result); 
     } 
    } 
    /** 
    * Background Async Task to Load all product by making HTTP Request 
    * */ 
    class LoadAllProducts extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(MapActivity.this); 
      pDialog.setMessage("....."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting All products from url 
     * */ 
     protected String doInBackground(String... args) { 
      return null; 
     } 
     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String result) { 
      for(int i; i < 3; i++){ 
       downloadTask = new DownloadTask(); 
       downloadTask.execute(result); 
      } 
     } 
    } 
} 
+0

是什麼問題? – 2014-09-04 08:09:58

+0

我不知道是否所有的ParserTask都完成了。如果它們沒有在arrayList中完成並不是全部結果。 – user3499866 2014-09-04 08:11:19

+0

你可以使用線程....爲該研究等待和通知..創建一個主線程,將等待所有線程完成他們的任務,當他們完成任務主線程將被通知。 – 2014-09-04 09:10:23

回答

0

爲什麼不直接在最後一個asyncTask的onPostExecute中調用您的活動方法來運行。

public class MapActivity extends FragmentActivity { 


    boolean areTasksComplete = false; 
    //call this when last task is complete 
    public void afterAllTasksComplete(List<List<HashMap<String, String>>> result) { 
     areTasksComplete = true; 
     dists.add(result); 

    } 

    private class DownloadTask extends AsyncTask<String, Void, String> 
    { 
     // Downloading data in non-ui thread 
     @Override 
     protected String doInBackground(String... url){} 

     // Executes in UI thread, after the execution of doInBackground() 
     @Override 
     protected void onPostExecute(String result){ 
      super.onPostExecute(result); 
      parserTask.execute(result); 
     } 
    } 

    /** A class to parse the Google Places in JSON format */ 
    private class ParserTask extends AsyncTask<String, Integer, List<List<HashMap<String, String>>>>{ 
     // Parsing the data in non-ui thread 
     @Override 
     protected List<List<HashMap<String, String>>> doInBackground(String... jsonData){ 
     } 

     // Executes in UI thread, after the parsing process 
     @Override 
     protected void onPostExecute(List<List<HashMap<String, String>>> result){ 

      afterAllTasksComplete(result); 
     } 
    } 
    /** 
    * Background Async Task to Load all product by making HTTP Request 
    * */ 
    class LoadAllProducts extends AsyncTask<String, String, String> { 

     //.....// 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String result) { 
      for(int i; i < 3; i++){ 
       downloadTask = new DownloadTask(); 
       downloadTask.execute(result); 
      } 
     } 
    } 

} 

此外,還有一個名爲BoltsFramework的庫,它使鏈接異步工作更容易。 https://github.com/BoltsFramework/Bolts-Android

0

你應該嘗試使用onPostExecute

從技術上講,沒有內建機制來檢查所有任務是否完成,因此如果一個任務完成,如果完成,檢查另一個等等,您可以嘗試每隔一段時間檢查一次,而重置機制,如果他們中的任何一個沒有完成。

用簡單的話說 - 每隔一段時間檢查一次,直到一切完成。