當然,我的代碼中刪除了一些不重要的部分。這裏是擴展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);
}
}
}
}
是什麼問題? – 2014-09-04 08:09:58
我不知道是否所有的ParserTask都完成了。如果它們沒有在arrayList中完成並不是全部結果。 – user3499866 2014-09-04 08:11:19
你可以使用線程....爲該研究等待和通知..創建一個主線程,將等待所有線程完成他們的任務,當他們完成任務主線程將被通知。 – 2014-09-04 09:10:23