I'm retrieving Hospital Data from firestore. I want to keep retrieved data to be global for the entire fragment, but the Map will return null after coming out from addOnSuccessListner().
I've declared the Map inside onCreateView()
static Map<String, Map<String, String>> hospitals ;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
hospitals = new HashMap<>();
.......
.....
return root;
}
Below is the code where I'm retrieving HospitalData.
private void readHospitalData() {
CollectionReference collectionReference = fStore.collection("hospital Details");
collectionReference.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for(QueryDocumentSnapshot queryDocumentSnapshot: queryDocumentSnapshots){
Map<String, String> map = new HashMap<>();
map.put("h_name", queryDocumentSnapshot.getString("h_name"));
map.put("h_email", queryDocumentSnapshot.getString("h_email"));
map.put("h_phone", queryDocumentSnapshot.getString("h_phone"));
map.put("h_road", queryDocumentSnapshot.getString("h_road"));
map.put("h_area", queryDocumentSnapshot.getString("h_area"));
map.put("h_city", queryDocumentSnapshot.getString("h_city"));
map.put("h_state", queryDocumentSnapshot.getString("h_state"));
map.put("h_pincode", queryDocumentSnapshot.getString("h_pincode"));
hospitals.put(queryDocumentSnapshot.getId(), map);
}
Log.d("MAP-->" , hospitals.toString()); // display the Map
}
});
Log.d("MAP-->" , hospitals.toString()); // but here it will display null
}