I've made a List of POJO Objects that get values from my Cloud Firestore database. This List is itself a Class having in mind to retrieve attributes everytime I create a new List. The thing is that the List is not filled in the on create activity but time after. I've tried adding loops, tasks, waits, but not getting the result desired. Here is how I retrieve my data satisfactorally:
private void retrieveData(){
reference = firestore.collection("attractions");
reference.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
@Nullable FirebaseFirestoreException e) {
attractionList.clear();
for (DocumentSnapshot snapshot : queryDocumentSnapshots) {
Attraction attraction = snapshot.toObject(Attraction.class);
addAttractions(attraction);
}
}
});
}
I retrieve the data everytime I create a new Object:
public AttractionList() {
firestore.setFirestoreSettings(settings);
reference = firestore.collection("attractions");
attractionList = new ArrayList<>();
retrieveData();
}
Then, if I check the List size on my Activity on create:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attraction);
Toolbar toolbar = findViewById(R.id.tb_attraction);
setSupportActionBar(toolbar);
//Creating POJO List and retrieving data
attractionList = new AttractionList();
Log.e("List Size: ", ""+attractionList.getAttractionList().size());
//List Size: 0
}
But if I use the same Log time after, in any button:
@Override
public void onBackPressed(){
Log.e("List Size: ", ""+attractionList.getAttractionList().size());
//List Size: 16
}
I don't really want to retrieve data in every single Activity or creating intents to bring the Object between Activities. There is any way I could fix this?
PS: This is my first post. Sorry If I've haven't done it on the right way. Thank's a lot.