Passing arguments to AsyncTask, and returning results
Change your method to look like this:
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
And change your async task implementation
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:
If you want to have access to the task starting context, the easiest way would be to override onPostExecute in place:
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);
Why would you pass an ArrayList??It should be possible to just call execute with the params directly:
String curloc = current.toString();String itemdesc = item.mDescription;new calc_stanica().execute(itemdesc, curloc)
That how varrargs work, right?Making an ArrayList to pass the variable is double work.
I sort of agree with leander on this one.
call:
new calc_stanica().execute(stringList.toArray(new String[stringList.size()]));
task:
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 }}
Or you could just make the result list a class parameter and replace the ArrayList with a boolean (success/failure);
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 }}