So I have been trying to download data from Firebase into an ArrayList:
public void updateOnce(){
animalList = new ArrayList<>();
Query query = mDatabase.orderByChild("id");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot messageSnapshot: dataSnapshot.getChildren()) {
SpottedAnimal pet = messageSnapshot.getValue(SpottedAnimal.class);
animalList.add(pet);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Debugger shows that the Object SpottedAnimal pet does get created and then inserted into the animalList that is a global ArrayList. But once I call the method:
public ArrayList<SpottedAnimal> getList(){
return animalList;
}
The animalList comes out empty on the other side.
FirebaseGetList() animalListHook = new FirebaseGetList();
animalListHook.updateOnce();
ArrayList<SpottedAnimal> animalList = animalListHook.getList();