Retrieving data works but I can't save the retrieved data into an ArrayList. Right after the "onDataChanged()" method ArrayList "profile" seems to have 2 values, but on the return statement it has 0.
static List<Profile> profiles = new ArrayList<Profile>();
static DatabaseReference dbr;
public static List<Profile> loadProfiles(Context context){
dbr = FirebaseDatabase.getInstance().getReference().child("users").child("hiring");
dbr.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
//String value = dataSnapshot.getValue(String.class);
//Log.d("hello", "Value is: " + value);
List<Profile> profiles2 = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Profile profile = snapshot.getValue(Profile.class);
//Log.d("hello", profile.getCompanyName());
profiles2.add(profile);
}
profiles = profiles2;
dbr.removeEventListener(this);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("hello", "Failed to read value.", error.toException());
}
});
return profiles;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…