[英]How do I make sure that it is connected() before any method is invoked? My methods are beight called AFTER the GooglePlus authentication
[英]How can I make sure that my ArrayList is filled in the OnCreate Method before being called in OnMapReady?
我正在從Firestore數據庫中檢索數據,並希望在Google地圖上為每個檢索到的位置添加標記。 但是,我的問題是,在我實際上可以使用檢索到的位置填充ArrayList之前,先調用OnMapReady方法。
我發現通過使用Breakpoints對其進行測試時,由OnMapReady方法調用時,我的ArrayList尚未填充。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
kundenList = new ArrayList<>(); //
db = FirebaseFirestore.getInstance();
db.collection("Kunden").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if(!queryDocumentSnapshots.isEmpty()){
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for(DocumentSnapshot d : list){
Kunde k = d.toObject(Kunde.class);
kundenList.add(k);//Here I fill my ArrayList with the values from the Database (Which Works as Intented)
}
}
}
});
map = findViewById(R.id.mapView);
map.onCreate(mapViewBundle);
map.getMapAsync(this);
}
public void onMapReady(GoogleMap map) {
map.setMyLocationEnabled(true);
if(!kundenList.isEmpty()){ //Here is the Problem. My ArrayList is empty (size = 0) when the code reaches this point and only gets filled afterwards
for(Kunde kunde : kundenList) {
MarkerOptions options = markLocations(kunde.getLat(), kunde.getLng(), kunde.getName());
map.addMarker(options);
}
}
}
我想在地圖上為從數據庫中檢索到的每個位置設置標記,但是我的代碼不會添加任何標記,因為調用時我的ArrayList為空。
完成填充列表后,您可能需要手動調用onMapReady(),如下所示:
for(DocumentSnapshot d : list){
Kunde k = d.toObject(Kunde.class);
kundenList.add(k);//Here I fill my ArrayList with the values from the Database (Which Works as Intented)
}
onMapReady(this.map);
這意味着,onMapReady可能會連續兩次被調用,但這對用戶應該是透明的。
我創建了一個新的方法callMap(),並在列表填充后調用它,這對我來說很有效。
db.collection("Kunden").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if(!queryDocumentSnapshots.isEmpty()){
List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
for(DocumentSnapshot d : list){
Kunde k = d.toObject(Kunde.class);
kundenList.add(k);
}
callMap();
}
}
});
public void callMap(){
map.getMapAsync(this);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.