我有一个执行一些长时间计算的应用程序,我想在完成时显示一个进度对话框。到目前为止,我发现我可以使用线程/处理程序来做到这一点,但是没有用,然后我发现了AsyncTask
.
在我的应用程序中,我使用带有标记的地图,并且我已经实现了 onTap 函数来调用我定义的方法。AsyncTask
该方法创建一个带有是/否按钮的对话框,如果单击是,我想调用一个。我的问题是如何将 an 传递ArrayList<String>
给AsyncTask
(并在那里使用它),以及如何ArrayList<String>
从AsyncTask
?
该方法的代码如下所示:
String curloc = current.toString();
String itemdesc = item.mDescription;
ArrayList<String> passing = new ArrayList<String>();
passing.add(itemdesc);
passing.add(curloc);
ArrayList<String> result = new ArrayList<String>();
new calc_stanica().execute(passing,result);
String minim = result.get(0);
int min = Integer.parseInt(minim);
String glons = result.get(1);
String glats = result.get(2);
double glon = Double.parseDouble(glons);
double glat = Double.parseDouble(glats);
GeoPoint g = new GeoPoint(glon, glat);
String korisni_linii = result.get(3);
因此,如您所见,我想将字符串数组列表“传递”到AsyncTask
,并从中获取“结果”字符串数组列表。calc_stanicaAssycTask
类看起来像这样:
public class calc_stanica extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(baraj_mapa.this);
dialog.setTitle("Calculating...");
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
}
protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
//Some calculations...
return something; //???
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
所以我的问题是如何在AsyncTask doInBackground
方法中获取“传递”数组列表的元素(并在那里使用它们),以及如何返回一个数组列表以在主方法中使用(“结果”数组列表)?