将parameter passing给AsyncTask,并返回结果

我有一个应用程序做了一些长时间的计算,我想在这个过程中显示一个进度对话框。 到目前为止,我发现我可以用线程/处理程序来做到这一点,但没有工作,然后我发现有关AsyncTask

在我的应用程序中,我使用带有标记的贴图,并且实现了onTap函数来调用我定义的方法。 该方法创build一个与是/否button的对话框,我想调用一个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); 

所以,如你所见,我想将string数组列表“传递”给AsyncTask ,并从中得到“结果”string数组列表。 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(); } 

所以我的问题是如何获得AsyncTask doInBackground方法中的“传递”数组列表中的AsyncTask doInBackground (并在那里使用它们),以及如何返回在主方法(“结果”数组列表)中使用的数组列表?

改变你的方法看起来像这样:

 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 

并改变你的asynchronous任务实施

 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); } 

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); 

你为什么要传递一个ArrayList? 应该可以直接调用params来执行:

 String curloc = current.toString(); String itemdesc = item.mDescription; new calc_stanica().execute(itemdesc, curloc) 

那varrargs是如何工作的,对吗? 使ArrayList传递variables是双重工作。

我有点赞同利安德这个人。

呼叫:

 new calc_stanica().execute(stringList.toArray(new String[stringList.size()])); 

任务:

 public class calc_stanica extends AsyncTask<String, Void, ArrayList<String>> { @Override protected ArrayList<String> doInBackground(String... args) { ... } @Override protected void onPostExecute(ArrayList<String> result) { ... //do something with the result list here } } 

或者你可以做结果列表一个类参数,并用布尔值(成功/失败)replaceArrayList;

 public class calc_stanica extends AsyncTask<String, Void, Boolean> { private List<String> resultList; @Override protected boolean doInBackground(String... args) { ... } @Override protected void onPostExecute(boolean success) { ... //if successfull, do something with the result list here } } 

我不这样做。 我发现重载asychtask类的构造函数更容易

公共类calc_stanica扩展AsyncTask>

 String String mWhateveryouwantToPass; public calc_stanica( String whateveryouwantToPass) { this.String mWhateveryouwantToPass = String whateveryouwantToPass; } /*Now you can use whateveryouwantToPass in the entire asynchTask ... you could pass in a context to your activity and try that too.*/ ... ... 

你可以得到像这样的返回结果: AsyncTask

 @Override protected Boolean doInBackground(Void... params) { if (host.isEmpty() || dbName.isEmpty() || user.isEmpty() || pass.isEmpty() || port.isEmpty()) { try { throw new SQLException("Database credentials missing"); } catch (SQLException e) { e.printStackTrace(); } } try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try { this.conn = DriverManager.getConnection(this.host + ':' + this.port + '/' + this.dbName, this.user, this.pass); } catch (SQLException e) { e.printStackTrace(); } return true; } 

接受课堂:

 _store.execute(); boolean result =_store.get(); 

希望这会有所帮助。