【问题标题】:AsyncTask inside of onPostExecute of another Asynctask and return the result另一个 Asynctask 的 onPostExecute 内部的 AsyncTask 并返回结果
【发布时间】:2016-04-27 14:41:44
【问题描述】:

在我的第一个 AsyncTask doInBackground 方法中,我运行了一个从 Google Place Api 获取地点列表的方法。在第一个 AsyncTask 的 postExecute 中,我得到了这些地方的所有名称,并将它们全部显示在 ListView 中。

我现在想显示一个地方距我当前位置的行驶距离(我已经可以得到它)。为此,我在另一个类中创建了另一个 AsyncTask 来获得这个距离。代码如下:

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

这是我的第一个 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);
                    }

我的问题是如何在我的 postExecute 中执行“距离 AsyncTask”并将其结果返回到我的第一个 AsyncTask 中,以便在我的 ListView 中显示它。

【问题讨论】:

    标签: java android listview android-asynctask google-places-api


    【解决方案1】:

    你可以这样做:

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

    另外,您不需要使用runOnUiThread(...),因为onPostExecute(...) 方法是在UI 线程上执行的。

    【讨论】:

    • 我不明白我必须把你的代码放在哪里,就在for之前?然后把其余的都放在onPostExecute(String result2) 里面(HashMap 等等)?
    • @user6262006 我的回答说明了如何在另一个任务完成时启动AsyncTask,以及如何在第二个任务的onPostExecute(...) 方法中访问第一个任务的结果。我不确定你想要达到什么目的,所以我不能更具体地说明你如何使用它。
    • 正如您在我发布的代码中看到的那样,对于Place nearPlaces.result 列表中的每个地方,我需要执行Distance AsyncTask,它在另一个类中,而不是在同一个活动中,并获得每个地点的Distance AsyncTask 的结果,因此我可以将其添加到我的HashMap 并用相应地点的名称显示它。你可以在我的代码中看到的postExecute 方法来自另一个 AsyncTask,它用于搜索地点。
    • @user6262006 您可以在创建Distance 对象的类中覆盖onPostExecute 方法,此方法不需要在Distance 类中。此外,在第一个任务完成后,您可以创建适配器并将其设置到列表中,然后,当每个 Distance 任务完成时,更新 placesListItems 列表 (placesListItems.add(map);) 并调用 adapter.notifyDataSetChanged() 以便这些更改将反映在 GUI 中。
    • 您是否找到了一个好的解决方案?我正在尝试或多或少做同样的事情...stackoverflow.com/questions/37553554/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多