我的代码当然会删除一些不重要的部分。这里有3个类,扩展了AsyncTask。 LoadAllProducts执行三个DownloadTask,每个DownloadTask执行ParserTask和ParserTask填充ArrayList。毕竟我想要使用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 :(得分:0)
您应该尝试使用onPostExecute。
从技术上讲,没有内置机制来检查所有任务是否都已完成,所以你可以尝试每一个任务完成,如果一个任务完成,如果完成,检查另一个等等,同时重置机制如果其中任何一个没有完成。
用简单的话说 - 经常检查,直到一切都完成。
答案 1 :(得分: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