我有 4 个值试图从 xml 文件放入哈希图中。我可以解析 xml 没问题,但想通过纬度/经度获取数据以将点放在地图上。我正在尝试从 pullparser 填充哈希图,但失败了。我遇到了关于在哪里填充我认为的哈希图的问题。
这是我的哈希图:
Map<Integer, MapMarkers> DataLocations = new HashMap<Integer, MapMarkers>();
这是我的 MapMarkers 类:
public class MapMarkers {
private String lon;
private String lat;
private String title;
private String desc;
public MapMarkers() {
super();
}
public MapMarkers(String lon, String lat, String title, String desc) {
super();
this.lon = lon;
this.lat = lat;
this.title = title;
this.desc = desc;
}
public String getLon() {
return lon;
}
public void setLon(String lon) {
this.lon = lon;
}
public String getLat() {
return lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
这是我的 pullparser 异步运行,因为 xml 很大:
公共类 BuildMapInfoTask 扩展 AsyncTask { String sname = null; 字符串 sdesc = null; 字符串 slat = null; 字符串 slon = null;
@Override
protected String doInBackground(String... params) {
try {
XmlPullParserFactory factory = XmlPullParserFactory
.newInstance();
factory.setValidating(false);
XmlPullParser myxml = factory.newPullParser();
FileInputStream fs = new FileInputStream(
"/storage/emulated/0/snoteldata/kml/snotelwithlabels.kml");
myxml.setInput(fs, null);
int eventType = myxml.getEventType();
int uniquekey = 0;
marks = new MapMarkers(slat, slon, sname, sdesc);
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
String tag = myxml.getName();
if ("name".equals(tag)) {
sname = myxml.nextText().trim();
marks.setTitle(sname);
} else if ("description".equals(tag)) {
sdesc = myxml.nextText().trim();
marks.setDesc(sdesc);
} else if ("longitude".equals(tag)) {
slon = myxml.nextText().trim();
marks.setLon(slon);
} else if ("latitude".equals(tag)) {
slat = myxml.nextText().trim();
marks.setLat(slat);
}
Log.v("ET",uniquekey+"");
DataLocations.put(uniquekey, marks);
uniquekey++;
}
eventType = myxml.next();
}
} catch (XmlPullParserException e) {
Log.e("PP Error", e.getMessage());
} catch (IOException e) {
Log.e("PP IOException", e.getMessage());
}
return null;
}
我创建了 uniquekey 作为 KEY,然后在 Values 区域中加载 lat,lon,title,desc ...我的值没有被填充。我需要在 PullParser 中的哪里使用 DataLocations.put(...) 来填充 DataLocations,如果我目前已经填充了它,我该如何提取它?我错了吗?