Firebase-第三个子对象中的getValue返回null

时间:2018-06-21 10:33:50

标签: java android firebase firebase-realtime-database

我有一个问题,当我想从Firebase获取值时,getValue()返回null

DataSnapshot appleSnapshotDataSnapshot dataSnapshot-在控制台中,我也可以在Firebase中正确看到键和值

数据库结构

enter image description here

调试

enter image description here

DataSnapshot setting-返回键,但不返回值(空)

String settingKey-正确返回字符串键

String settingValue-返回空值

为什么我不能从较深的孩子那里获得价值?

private void readData(final ValidateJoinInterface validateJoinInterface, final String name, final String password, final String user) {

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    ref.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot appleSnapshot : dataSnapshot.getChildren()) {

                DataSnapshot setting = appleSnapshot.child(name).child("Settings").child("Password");
                String settingKey = (String) appleSnapshot.child(name).child("Settings").child("Password").getKey();
                String settingValue = (String) appleSnapshot.child(name).child("Settings").child("Password").getValue();

            }
            validateJoinInterface.validateJoin(correctName, correctPassword, correctUserId);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.e("DTAG", "onCancelled", databaseError.toException());
        }
    });
}

2 个答案:

答案 0 :(得分:0)

您可以获取任何值:

FirebaseDatabase.getInstance().getReference().child(name).child("Settings").child("Password")
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
                            Log.d(TAG,"KEY - "+messageSnapshot.getKey() + "VALUE - "+messageSnapshot.getValue());
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Crashlytics.logException(databaseError.toException());
                        sendMessageListener.onSendMessageFailure(databaseError.getMessage());
                    }
                });

答案 1 :(得分:0)

假设name变量像您的示例Aa一样具有字符串值,并且假设Aa是Firebase根的直接子代,要解决此问题,请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference settingsRef = rootRef.child(name).child("Settings");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
            String name = dataSnapshot.child("Name").getValue(String.class);
            String password = dataSnapshot.child("Password").getValue(String.class);
            Log.d("TAG", name + " / " + password);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
settingsRef.addListenerForSingleValueEvent(valueEventListener);

输出将为:Aa / Aaa