Boj*_*ski 37 android return progressdialog android-asynctask
我有一个应用程序做了一些长时间的计算,我想在完成后显示进度对话框.到目前为止,我发现我可以使用线程/处理程序执行此操作,但是没有工作,然后我发现了有关AsyncTask.
在我的应用程序中,我使用带有标记的地图,并且我已经实现了onTap函数来调用我已经定义的方法.该方法创建一个带有"是/否"按钮的对话框,AsyncTask如果单击"是" ,我想调用它.我的问题是如何传递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);
Run Code Online (Sandbox Code Playgroud)
所以,如你所见,我想将字符串数组列表"传递"给它AsyncTask,并从中获取"结果"字符串数组列表.calc_stanica AssycTask类看起来像这样:
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();
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是如何在方法中获取"传递"数组列表的元素AsyncTask doInBackground(并在那里使用它们),以及如何返回要在main方法中使用的数组列表("结果"数组列表)?
Kon*_*rov 66
将您的方法更改为如下所示:
String curloc = current.toString();
String itemdesc = item.mDescription;
ArrayList<String> passing = new ArrayList<String>();
passing.add(itemdesc);
passing.add(curloc);
new calc_stanica().execute(passing); //no need to pass in result list
Run Code Online (Sandbox Code Playgroud)
并更改您的异步任务实现
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) {
ArrayList<String> result = new ArrayList<String>();
ArrayList<String> passed = passing[0]; //get passed arraylist
//Some calculations...
return result; //return result
}
protected void onPostExecute(ArrayList<String> result) {
dialog.dismiss();
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);
}
Run Code Online (Sandbox Code Playgroud)
UPD:
如果您想要访问任务启动上下文,最简单的方法是覆盖onPostExecute:
new calc_stanica() {
protected void onPostExecute(ArrayList<String> result) {
// here you have access to the context in which execute was called in first place.
// You'll have to mark all the local variables final though..
}
}.execute(passing);
Run Code Online (Sandbox Code Playgroud)
Lea*_*der 12
你为什么要传递一个ArrayList?应该可以直接使用params调用execute:
String curloc = current.toString();
String itemdesc = item.mDescription;
new calc_stanica().execute(itemdesc, curloc)
Run Code Online (Sandbox Code Playgroud)
varrargs是如何工作的,对吧?使ArrayList传递变量是双重工作.
| 归档时间: |
|
| 查看次数: |
98907 次 |
| 最近记录: |