【发布时间】: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