What I am trying to do is load a list of values (Usernames) from a child directory in Firebase. I have a method createProviderList(int locGrid)
that returns the arraylist of user data. Since it is asynchronous however, it executes the return statement before the data is read. How do I overcome this? I apologize for my ignorance as I am fairly new to Android/Java. Can someone please explain how to handle this?
Just to be clear, all of this code does what I need except that the return providerList;
is executed before any of the elements of the ArrayList are added.
public static ArrayList<ProviderInfo> createProviderList(int locGrid) {
ArrayList<ProviderInfo> providerList = new ArrayList<ProviderInfo>();
ArrayList<String> availableProviders = new ArrayList<String>();
FirebaseAppUtilsHSquared fbu = new FirebaseAppUtilsHSquared();
DatabaseReference db = FirebaseDatabase.getInstance().getReference().child(MainActivity.GPS_PATH)
.child(String.valueOf(locGrid));
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot ds : snapshot.getChildren()){
providerList.add(fbu.getUserProfileInfo(String.valueOf(ds.getValue())));
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return providerList;