【发布时间】:2016-11-07 14:02:10
【问题描述】:
如何正确返回列表?
我正在使用 OSMdroid 编写一个应用程序,我想使用“Place”类的变量“经度”和“纬度”(在这篇文章的底部)最终在“onPostExecute”方法中使用它们,对吧我在其中设置了“PLACEHOLDERS”。
Android Studio 想让我修改“return loadXmlFromNetwork(urls[0]);”这行代码,它会执行下面的代码,但我不知道具体如何(下面的方法在同一个类中)。
private class DownloadXmlTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
//Here I want to recieve the list
return loadXmlFromNetwork(urls[0]);
} catch (IOException e) {
return getResources().getString(R.string.connection_error);
} catch (XmlPullParserException e) {
return getResources().getString(R.string.xml_error);
}
}
@Override
protected void onPostExecute(List<Place> result) {
MapView map = (MapView) findViewById(R.id.map);
IMapController mapController = map.getController();
mapController.setZoom(17);
//Here I want to use the latitude and longitude variables of the List
GeoPoint myLocation = new GeoPoint(PLACEHOLDER(latitude), PLACEHOLDER(longitude));
mapController.animateTo(myLocation);
}
}
这是我第一次收到名单的地方:
private List<Place> loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
InputStream stream = null; // Instantiate the parser
XMLParser XMLParser = new XMLParser();
List<Place> places = null;
try {
stream = downloadUrl(urlString);
places = XMLParser.parse(stream); // Makes sure that the InputStream is closed after the app is finished using it.
} finally {
if (stream != null) {
stream.close();
}
}
return places;
}
这是我的 Place 课程:
public class Place {
private String longitude;
private String latitude;
private String place_id;
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getPlace_id() {
return place_id;
}
public void setPlace_id(String place_id) {this.place_id = place_id;}
@Override
public String toString() {
return "ID: " + place_id + "\n" + "Longitude: " + longitude + "\n" + "Latitude: " + latitude;
}
【问题讨论】: