如何从firebase android检索用户数据

如何从firebase android检索用户数据,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我已经创建了一个配置文件页面,但无法检索存储在用户下作为根节点的用户详细信息。每个用户都存储在它的userid节点下,但我只检索用户的概要图片,而不检索其余的细节- 这是我的DisplayProfile.java代码 public class DisplayProfile extends AppCompatActivity { private static final String TAG = "DisplayProfile"; public static final String Fireba

我已经创建了一个配置文件页面,但无法检索存储在用户下作为根节点的用户详细信息。每个用户都存储在它的userid节点下,但我只检索用户的概要图片,而不检索其余的细节-

这是我的DisplayProfile.java代码

public class DisplayProfile extends AppCompatActivity {
private static final String TAG = "DisplayProfile";
public static final String Firebase_Server_URL = "https://gakusei-go.firebaseio.com/";


private ImageView profilePic;
private TextView profileName, profilePhone, profileEmail;
private Button profileUpdate;
private FirebaseAuth firebaseAuth;
private FirebaseDatabase firebaseDatabase;
private FirebaseStorage firebaseStorage;
private String userID;
FirebaseUser user;
private FirebaseAuth.AuthStateListener mAuthListener;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_profile);

    profileUpdate=(Button)findViewById(R.id.pbuttonSave);
    profilePic = findViewById(R.id.ivProfilePic);
    profileName = findViewById(R.id.tvProfileName);
    profilePhone = findViewById(R.id.tvProfilePhone);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    FirebaseUser user=FirebaseAuth.getInstance().getCurrentUser();

    System.out.println(userID);
    firebaseStorage = FirebaseStorage.getInstance();
    userID = firebaseAuth.getUid();
    System.out.println(userID);
    profileUpdate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            startActivity(new Intent(DisplayProfile.this, SecondActivity.class));

        }

    })
    mAuthListener = new FirebaseAuth.AuthStateListener() {
       @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {

            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
                // Toast.makeText(getApplicationContext(),"Successfully signed in with: " + user.getEmail());
                Toast.makeText(getApplicationContext(), "Enter email address!" +user.getEmail(), Toast.LENGTH_SHORT).show();
            } else {
                                  Log.d(TAG, "onAuthStateChanged:signed_out");
                Toast.makeText(getApplicationContext(), "Successfuly signed out", Toast.LENGTH_SHORT).show();
            }
        }
    };
    StorageReference storageReference = firebaseStorage.getReference();
    storageReference.child("images").child(firebaseAuth.getUid()+ "." + "jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            Picasso.get().load(uri).fit().centerCrop().into(profilePic);
        }
    });
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users");
    databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot Mainsnapshot) {

            for (DataSnapshot datasnapShot : Mainsnapshot.getChildren()) {

                Users userProfile = datasnapShot.getValue(Users.class);
                profileName.setText("Name: " + userProfile.getName());
                profilePhone.setText("Age: " + userProfile.getPhone());
                profileEmail.setText("Email: " + userProfile.getEmail());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(DisplayProfile.this, databaseError.getCode(), Toast.LENGTH_SHORT).show();
        }
    });
  }
 }  
}

我的展示活动就在这里

    package com.example.deepak.myapplication;


   public class Users {
   public String imageUrl;
   public String imageName;
   public String uid;
   public String name;
   public String email;
   public String password;
   public String phone;

public Users() {
}


public Users(String uid, String name, String email, String password, String phone) {
    this.uid = uid;
    this.name = name;
    this.email = email;
    this.password = password;
    this.phone = phone;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}
     <?xml version="1.0" encoding="utf-8"?>
     <android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_image_upload"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical">

<ImageView
    android:id="@+id/ivProfilePic"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.238"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    app:srcCompat="@mipmap/ic_launcher" />

<Button
    android:id="@+id/pbuttonSave"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="140dp"
    android:text="Edit"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvProfilePhone" />


<TextView

    android:id="@+id/tvProfileName"
    android:layout_width="wrap_content"
    android:layout_height="36dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="208dp"
    android:textSize="28dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.401"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/ivProfilePic" />


<TextView
    android:id="@+id/tvProfilePhone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Phone: "
    android:textSize="28dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.378"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tvProfileName"
    app:layout_constraintVertical_bias="0.391" />


<TextView
    android:id="@+id/tvProfileEmail"
    android:layout_width="69dp"
    android:layout_height="41dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="160dp"
    android:text="Email"
    android:textSize="22dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.384"
    app:layout_constraintStart_toStartOf="parent" />
    </android.support.constraint.ConstraintLayout>


根据您的数据库,您有:

 users
     userId 
         name : user_name
         phone : user_phone 
然后,您需要执行以下操作:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener()
在您的代码中有
getReference(“user”)
,然后您编写了
child(“users”)
,这意味着您正在访问数据库中不存在的节点


完整代码:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot mainSnapshot) {

            Users userProfile = mainSnapshot.getValue(Users.class);
            profileName.setText("Name: " + userProfile.getName());
            profilePhone.setText("Phone: " + userProfile.getPhone());
            profileEmail.setText("Email: " + userProfile.getEmail());
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

错误出现在
getReference(“用户”)
中,然后进一步引用子
users


但是您仍然能够获取配置文件图像,因为您在获取图像时直接引用了云存储,您已经知道配置文件图像的名称为
gs:///images/.jpg
(不使用RTDB中用户数据中的
imageURL
值)

发布数据库结构。另外,请仅将您试图从Firebase数据库检索的代码放在其中,发布不需要的全部代码,使问题更难阅读。添加了到我的数据库的链接。然后,您可以帮助我在更改引用后如何获取用户详细信息吗。因为即使有人按照上面提到的解决方案更改了引用,但仍然没有获得任何详细信息。您是否发布了数据库规则?()所有内容都被定义为公共的,您可以在提到数据库的位置将数据库规则定义为公共的,这可能是因为您将行
.child(“用户”)
更改为
.child(“用户ID”)
。请放慢代码的速度,仔细考虑这些变化以及它们的含义。那么,您能建议我如何进一步操作吗?完全按照我在编辑中所写的复制代码,您还有其他错误..您尝试过吗?java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'在com.example.deepak.myapplication.DisplayProfile$4.onDataChange(DisplayProfile.java:132)上的空对象引用上,查看您的代码,您没有初始化profileEmail,这就是为什么会出现空点异常。.执行此操作
profileEmail=findViewById(R.id.ivProfileEmail)id应该是xml中的id