2 回答
TA贡献1780条经验 获得超1个赞
要获取所有文档的值latitude和longitude属性,只需使用以下代码行:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference notesRef = rootRef.collection("notes");
notesRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
float lat = Float.parseFloat(document.getString("Latitude"));
float lng = Float.parseFloat(document.getString("Longitude"));
String name = document.getString("Attraction Name");
LatLng latLng = new LatLng(lat, lng);
mMaps.addMarker(new MarkerOptions().position(latLng).title(name));
}
}
}
});
所以解决这个问题的关键是使用task.getResult()that可以帮助你遍历QueryDocumentSnapshot对象。
TA贡献1875条经验 获得超3个赞
如果您想要所有文档数据,您可以尝试以下操作:-
db.collection("Chat").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges()) {
switch (dc.getType()) {
case ADDED:
// Log.d("TAG", "New Msg: " + dc.getDocument().toObject(Message.class));
break;
case MODIFIED:
// Log.d("TAG", "Modified Msg: " + dc.getDocument().toObject(Message.class));
break;
case REMOVED:
// Log.d("TAG", "Removed Msg: " + dc.getDocument().toObject(Message.class));
break;
}
}
}
});
添加回答
举报