I'm trying to implement Firebase Database in my Android app, but when it give back the array which is supposed to be fill with the names of the strings, it is void. This is the function that I use to get back the names:
public class FirebaseOperation {
private static final String TAG1 = "Operacion de lectura";
FirebaseDatabase database = FirebaseDatabase.getInstance();
private final Callback callback;
public FirebaseOperation(Callback callback){
this.callback = callback;
}
interface Callback{
void dataIsLoaded(ArrayList<String> names);
}
public void getUserNames(){
final ArrayList<String> names = new ArrayList<>();
DatabaseReference ref = database.getReference().child("idUsers");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()){
String name = ds.getValue(String.class);
names.add(name);
Log.d(TAG1, "LEcturea correcta");
}
callback.dataIsLoaded(names);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.w(TAG1, "Error al leer los nombres de usuario");
}
});
}
}
And here is where i called it:
idusers = new ArrayList<>();
new FirebaseOperation(new FirebaseOperation.Callback() {
@Override
public void dataIsLoaded(ArrayList<String> names) {
idusers = names;
}
}).getUsernames();
if(idusers.isEmpty()){
Log.w(TAG, "Array VACIOOOOO");
}
And it's supposed to return the ArrayList but when it returns is void, however in that function it enters in the onDataChange , so it's supposed to be Reading the values, but returns an empty array to the other class. Some ideas? I let you a capture of my database structure.
Some ideas??