android - Passing arguments to AsyncTask, and returning results -
i have application long calculations, , show progress dialog while done. far have found threads/handlers, didn't work, , found out asynctask
.
in application use maps markers on it, , have implemented ontap function call method have defined. method creates dialog yes/no buttons, , call asynctask
if yes clicked. question how pass arraylist<string>
asynctask
(and work there), , how new arraylist<string>
result asynctask
?
the code of method looks 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, see, send string array list "passing" asynctask
, , "result" string array list it. , calc_stanica assyctask
class looks 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 question how elements of "passing" array list in asynctask doinbackground
method (and use them there), , how return array list use in main method (the "result" array list)?
change method 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 pass in result list
and change 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 want have access task starting context, easiest way override onpostexecute in place:
new calc_stanica() { protected void onpostexecute(arraylist<string> result) { // here have access context in execute called in first place. // you'll have mark local variables final though.. } }.execute(passing);
Comments
Post a Comment