Java 无法从Firebase(Android)获取数据
我试图从google firebase获取数据,但每次我都试图获取数据。“Else”案例每次都在运行。试图更改if(dataSnapshot.exists()),但收到相同的错误。请帮助解决它Java 无法从Firebase(Android)获取数据,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我试图从google firebase获取数据,但每次我都试图获取数据。“Else”案例每次都在运行。试图更改if(dataSnapshot.exists()),但收到相同的错误。请帮助解决它 public void retrieveProfileInfo() { currentUserID = mAuth.getUid(); key = rootRef.child("profiles").push().getKey(); rootRef.child("users-pro
public void retrieveProfileInfo() {
currentUserID = mAuth.getUid();
key = rootRef.child("profiles").push().getKey();
rootRef.child("users-profiles").child(currentUserID).child(key)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
if ((dataSnapshot.child("name").getValue()) != null){
String retrieveName = dataSnapshot.child("name").getValue().toString();
String retrieveStatus = dataSnapshot.child("about").getValue().toString();
nameBox.setText(retrieveName);
aboutBox.setText(retrieveStatus);
} else {
Toast.makeText(UserProfileActivity.this, currentUserID+"else - retrieveProfileInfo()",
Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Log.i("retrieveProfileInfo : ", "error is : " + e);
Toast.makeText(UserProfileActivity.this, e+" : Error",
Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
您不能按任意键并从中读取,您需要在
用户配置文件/userID
下遍历子项:
public void retrieveProfileInfo() {
//current user ID
currentUserID = mAuth.getUid();
ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds:dataSnapshot.getChildren()){
//get the values
String name = ds.child("name").getValue(String.class);
String about = ds.child("about").getValue(String.class);
String uid = ds.child("uid").getValue(String.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// log error
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
rootRef.child("users-profiles").child(currentUserID).addValueEventListener(listener);
}