将参数传递给AsyncTask,并返回结果(Passing arguments to AsyncTask, and returning results)
我有一个应用程序可以进行一些长时间的计算,并且我希望在完成后显示进度对话框。 到目前为止,我发现我可以使用线程/处理程序来完成此操作,但没有奏效,然后我发现了
AsyncTask
。在我的应用程序中,我使用带有标记的贴图,并且实现了onTap函数来调用我定义的方法。 该方法用Yes / No按钮创建对话框,如果单击Yes,我想调用
AsyncTask
。 我的问题是如何传递一个ArrayList<String>
到AsyncTask
(并在那里工作),以及如何从AsyncTask
返回一个新的ArrayList<String>
?该方法的代码如下所示:
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
方法中的“传递”数组列表的元素(并在那里使用它们)以及如何返回在主方法中使用的数组列表(“结果”数组列表)?I have an application that does some long calculations, and I would like to show a progress dialog while this is done. So far I have found that I could do this with threads/handlers, but didn't work, and then I found out about the
AsyncTask
.In my application I use maps with markers on it, and I have implemented the onTap function to call a method that I have defined. The method creates a dialog with Yes/No buttons, and I would like to call an
AsyncTask
if Yes is clicked. My question is how to pass anArrayList<String>
to theAsyncTask
(and work with it there), and how to get back a newArrayList<String>
like a result from theAsyncTask
?The code of the method looks like this:
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);
So, as you see, I would like to send the string array list "passing" to the
AsyncTask
, and to get the "result" string array list back from it. And the calc_stanicaAssycTask
class looks like this: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(); }
So my question is how to get the elements of the "passing" array list in the
AsyncTask doInBackground
method (and use them there), and how to return an array list to use in the main method (the "result" array list)?
原文:https://stackoverflow.com/questions/4195609