简体   繁体   中英

AsyncTask inside of onPostExecute of another Asynctask and return the result

In my first AsyncTask doInBackground method I run a method that get a list of places from Google Place Api. Inside the postExecute of this first AsyncTask, I get all the names of these places and show them all in a ListView.

I now would like to show the driving distance of a single place from my current location (I can already get it). To do so, I created, in another class, another AsyncTask to get this distance. Here is the code:

public class Distance extends AsyncTask<Double, Double, String> {
    GooglePlaces googlePlaces;
    String distancePlace = null;
    @Override
    final protected String doInBackground(Double... params) {
        double lat1,lat2,lon1,lon2;
        lat1=params[0];
        lon1=params[1];
        lat2=params[2];
        lon2=params[3];
        googlePlaces = new GooglePlaces();
        distancePlace= googlePlaces.getDistance(lat1,lon1,lat2,lon2);
        return distancePlace;
    }
}

and this is the code of my first AsyncTask postExecute :

    protected void onPostExecute(String s) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //get json status
                String status = nearPlaces.status;
                if (status.equals("OK")){
                    if (nearPlaces.results != null){
                        //every single place
                        for (Place p : nearPlaces.results){
                        //just a try, here I would like to get the distance
                         /*
                            Double[] myparams = {gps.getLatitude(),gps.getLongitude(),
                                    p.geometry.location.lat,p.geometry.location.lng};
                            new Distance().execute(myparams);*/

                            HashMap<String,String> map = new HashMap<String, String>();
                                map.put(KEY_NAME,p.name);
                                //add hashmap
                                placesListItems.add(map);
                          }
                        ListAdapter adapter = new SimpleAdapter(GpsActivity.this, placesListItems, R.layout.list_item, new String[] {KEY_REFERENCE,KEY_NAME},
                                new int[] {R.id.reference, R.id.name});
                        //add into listview
                        lv.setAdapter(adapter);
                    }

My problem is how to execute the "distance AsyncTask" inside my postExecute and return its result into my first AsyncTask, to show it in my ListView.

You can do something like this:

Distance distance = new Distance(){
    protected void onPostExecute(final String result1) {
        // First AsyncTask result.
        Distance distance2 = new Distance(){
            protected void onPostExecute(String result2) {
                // process the second result. 
                // Because the first result "result1" is "final", 
                // it can be accessed inside this method.
            }
        };
        distance2.execute(...);
    }
};
distance.execute(...);

Also, you don't need to use runOnUiThread(...) because the onPostExecute(...) method is executed on the UI thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM