数据库结构:
应用程序名称/用户/用户ID /个人/名称
孩子的名字"在数据库中被更新,但返回为null。
mChildReference = FirebaseDatabase.getInstance().getReference().child("users").child(uid).child("personal"); //DatabaseReference
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
User post = dataSnapshot.getValue(User.class);
Toast.makeText(MainActivity.this, "Name: "+post.uname+"dob: "+post.udob+"phno: "+post.phone_number+"email: "+post.uemail, Toast.LENGTH_LONG).show();
Log.e("RETRIEVED DATA", "Name: "+post.getUname()+"dob: "+post.getDob()+"phno: "+post.getPhone_number()+"email: "+post.getEmail());
Log.e("DEBUG",dataSnapshot.getKey());
fname.setText(post.getUname());
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.e("DEBUG", "loadPost:onCancelled", databaseError.toException());
// [START_EXCLUDE]
Toast.makeText(MainActivity.this, "Failed to load post.",
Toast.LENGTH_SHORT).show();
// [END_EXCLUDE]
}
};
mChildReference.addValueEventListener(postListener);
mPostListener = postListener;
答案 0 :(得分:1)
如果数据库行事很糟糕,请确保仔细检查数据库规则。您现在可以尝试将它们公开。我在开发时觉得这很有用。
将所有权限设置为公开(警告任何人 可以读写!)
// These rules give anyone, even people who are not users of your app,
// read and write access to your database
{
"rules": {
".read": true,
".write": true
}
}