我有以下stop
文档结构:
这是输出
正如您所见,12:00PM
下的1:00PM
显示在asynchronous
下。我认为这是由于keys
请求。那我该怎么办呢?
实际上,首先我得到文档,然后得到times
离开的stops
然后得到那个时间的db.collection(DBMeta.COLLECTION_ROUTE)
.document(routeIntent.getId())
.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot,
@Nullable FirebaseFirestoreException e) {
Route route = null;
if(documentSnapshot!=null)
route = documentSnapshot.toObject(Route.class);
if(route != null){
if(route.getDeparture() != null){
String[] departures = route.getDeparture().keySet().toArray(new String[route.getDeparture().keySet().size()]);
for(String time : departures){
Object object = route.getDeparture().get(time).get(DBMeta.DOCUMENT_ROUTE_STOPS);
ArrayList<DocumentReference> stops = (ArrayList<DocumentReference>)object;
departureLayout.addView(timeTextView(time));
for(DocumentReference stop : stops){
stop.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
final String bussStop = documentSnapshot.getString(DBMeta.DOCUMENT_STOP_NAME);
DocumentReference doc = documentSnapshot.getDocumentReference(DBMeta.DOCUMENT_STOP_CITY);
if(doc != null){
doc.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
String cityAbbrev = documentSnapshot.getString(DBMeta.DOCUMENT_CITY_ABBREV);
departureLayout.addView(stopTextView(bussStop + " - " + cityAbbrev));
}
});
}
}
});
}
}
}
}
}
});
数组,现在再次发送请求获取停止名称和城市。
这是我用来获取时间和所有时间段的代码。
{{1}}
答案 0 :(得分:2)
要解决此问题,请使用以下代码:
db.collection(DBMeta.COLLECTION_ROUTE).document(routeIntent.getId()).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Map<String, Object> map = document.getData();
Map<String, Object> departureMap = (Map<String, Object>) map.get("departure");
for (Map.Entry<String, Object> entry : departureMap.entrySet()) {
Log.d(TAG, entry.getKey());
Map<String, Object> innerMap = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> e : innerMap.entrySet()) {
Log.d(TAG, e.getValue().toString());
}
}
}
}
}
});
输出将是:
12:00 PM
ByPass - GRW
1:00 PM
Jail Chowk - GRT
除非您需要获得实时更新,否则不要使用addSnapshotListener
。