简体   繁体   中英

How can I make sure that my ArrayList is filled in the OnCreate Method before being called in OnMapReady?

I'm retrieving Data from my Firestore Database and want to add Markers on my Google Map for each retrieved Location. However, my Problem is that the OnMapReady Method is called before I can actually fill an ArrayList with my retrieved Locations.

I found out that my ArrayList is not filled yet when being called by the OnMapReady Method by testing it out with Breakpoints.

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);         
        }
        }
    }

I want to have Markers on the Map for each Location retrieved from the Database but my Code won't add any Markers because my ArrayList is empty when called.

You'll probably want to manually call your onMapReady() once you have finished filling your list... like so:

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);

This means, that onMapReady may get called twice, in close succession, but that should be transparent to the user.

I made a new Method callMap() and call it after my List is filled, which works for me.

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM