Firebase读取操作在Android中返回了一个空数组

时间:2019-05-27 22:02:25

标签: android firebase firebase-realtime-database

我正在尝试在Android应用程序中实现Firebase数据库,但是当它返回应该用字符串名称填充的数组时,它是无效的。这是我用来取回名字的功能:

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");
        }
    });

}

}

这就是我叫它的地方:

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");
            }

它应该返回ArrayList,但是当它返回时为void,但是在该函数中它将输入onDataChange,因此它应该是在读取值,但是将空数组返回给另一个类。有什么想法吗?我让您捕获了我的数据库结构。

Database

一些想法?

1 个答案:

答案 0 :(得分:0)

由于new ValueEventListener()是一个异步回调,这意味着return nombres发生在Firebase RTD响应之前。

如果您想保留课程FirebaseOperation,则一旦检索到,您就必须使用另一个回调传递Firebase:

public class FirebaseOperation {

    private final Callback callback;

    //Initializing the callback on the constructor for simplicity
    public FirebaseOperaion(Callback callback) {
        this.callback = callback;
    }

    //It doesn't return
    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.rtdResponse(names);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.w(TAG1, "Error al leer los nombres de usuario");
        }
        });
    }


    interface Callback {
        void rtdResponse(List<String> names);
    }

}

现在在另一个使用它的类中,而不是

List<String> names = new FirebaseOperation().getUserNames();

您将必须使用回调:

//Use this if you want the other class implements the callback
new FirebaseOperation(this).getUserNames();
//Or use an anonymous implementation
new FirebaseOperation(new Callback {
    @Override
    void rtdResponse(List<String> names) {
        //Do something with your list here
    }
}).getUserNames();