我从数据库中提取数据并将其放入哈希映射中。
HashMap<String,Object> players= new HashMap();
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
players.put(childSnapshot.getKey(), childSnapshot.getValue());
}
for (Map.Entry<String, Object> entry : players.entrySet()) {
Log.d("asd","Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Log.d输出为:
D/asd: Key = lastpeony, Value = {lat=40.89, long=29.37, avatar=dino}
D/asd: Key = lifesuxtr, Value = {lat=40.8901765, long=29.377306, avatar =petyr}
我要做的是访问每个键的lat long和avatar值。 我怎么做 ? 因为稍后我将使用这些值在地图上绘制标记。
感谢
答案 0 :(得分:0)
如果要在HashMap<String,Object> players
地图的每个值中提取特定内容(latti或longi),则值类型应该比Object
类型更具体。因此,请使用HashMap<String,Object>
players
来存储HashMap<String,SomeSpecificType>
答案 1 :(得分:0)
您使用hashmap .get https://developer.android.com/reference/java/util/HashMap.html#get(java.lang.Object)
String key = "lastpeon";
Object value = players.get(key);
答案 2 :(得分:0)
更改
HashMap<String,Object> players= new HashMap();
到
HashMap<String,Map<String,Object>> players= new HashMap<>();
然后使用:
初始化Map
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
players.put(childSnapshot.getKey(), (Map<String,Object>)childSnapshot.getValue());
}
然后你可以遍历内部Map
s:
for (Map.Entry<String, Map<String,Object>> entry : players.entrySet()) {
for (Map.Entry<String, Object> ientry : entry.getValue().entrySet()) {
Log.d("asd","Key = " + ientry.getKey() + ", Value = " + ientry.getValue());
}
}