Firebase数据库子值在检索时为空

时间:2018-09-24 18:40:17

标签: java android firebase firebase-realtime-database

我正在尝试检索数据库值,但是使用uID提取的子项中的数据为空。

enter image description here

这是我的代码-

   Intent intent = getIntent();
                        Bundle extras = intent.getExtras();
                        currentUser = extras.getString("currentUser");
                        selectedUser = extras.getString("selectedUser");
                        uID = extras.getString("uID");
                mFirebaseDatabase = FirebaseDatabase.getInstance();


            mDbReference = FirebaseDatabase.getInstance().getReference();
                final DatabaseReference uidRef = mDbReference.child("users").child(uID);

                ValueEventListener valueEventListener = new ValueEventListener() {
             @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                DataSnapshot firstChild = ds.child(ChatMessageModel.MSG_TYPE_RECEIVED);
                for (DataSnapshot secondChild : firstChild.getChildren()) {
                    ChatMessageModel chatMessageModel = secondChild.getValue(ChatMessageModel.class);
                    messageList.add(chatMessageModel);
                    for (int i = 0; i < messageList.size(); i++) {
                        Log.d(TAG, "All received messages : " + messageList.get(i).getMsgContent());

                    }
                }

            }
                            chatAppMsgAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            Log.d(TAG, databaseError.getMessage());
                        }
                    };
                    uidRef.addListenerForSingleValueEvent(valueEventListener);

//这是我的数据库 u0TddklO8BXKCRHp4UFXEvmPS1H3:null // u0TddklO8BXKCRHp4UFXEvmPS1H3是uid。其中,uid是currentuser的。 // ans必须是-

  "users": {
           "User2" : {
  "MSG_TYPE_RECEIVED" : {
    "-LNEG2DhamuO6hAvZUp9" : {
      "msgContent" : "hello",
      "msgType" : "MSG_TYPE_RECEIVED"
    },
    "-LNEG8hxAcXp05g5RYly" : {
      "msgContent" : "Hi fi",
      "msgType" : "MSG_TYPE_RECEIVED"
    }
  }
}

}

1 个答案:

答案 0 :(得分:0)

从您的评论中了解 像这样的数据库

"users": {
           "User2" : {
  "MSG_TYPE_RECEIVED" : {
    "-LNEG2DhamuO6hAvZUp9" : {
      "msgContent" : "hello",
      "msgType" : "MSG_TYPE_RECEIVED"
    },
    "-LNEG8hxAcXp05g5RYly" : {
      "msgContent" : "Hi fi",
      "msgType" : "MSG_TYPE_RECEIVED"
    }
  }

将数据库更改为

"users": {
           "u0TddklO8BXKCRHp4UFXEvmPS1H3" : {
  "MSG_TYPE_RECEIVED" : {
    "-LNEG2DhamuO6hAvZUp9" : {
      "msgContent" : "hello",
      "msgType" : "MSG_TYPE_RECEIVED"
    },
    "-LNEG8hxAcXp05g5RYly" : {
      "msgContent" : "Hi fi",
      "msgType" : "MSG_TYPE_RECEIVED"
    }
  }
}
}