如何获取正确的DatabaseReference

huangapple go评论104阅读模式
英文:

How can I get the correct DatabaseReference

问题

USER PROFILE

private void showUserProfile(FirebaseUser firebaseUser) {

    String userID = firebaseUser.getUid();

    DatabaseReference referenceProfile = FirebaseDatabase.getInstance().getReference("Users");
    referenceProfile.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            ReadWriteUserDetails readUserDetail = snapshot.getValue(ReadWriteUserDetails.class);
            if (readUserDetail != null) {
                fullname = firebaseUser.getDisplayName();
                email = firebaseUser.getEmail();
                doB = readUserDetail.birthday;
                discount = readUserDetail.discount;

                textViewFullName.setText(fullname);
                textViewEmail.setText(email);
                textViewDoB.setText(doB);
                textViewDiscount.setText(discount);
            }
        }

Firebase

如何获取正确的DatabaseReference

ReadWriteUserDetail class

public class ReadWriteUserDetails {
    public String birthday, discount;

    public ReadWriteUserDetails() {
    }

    public ReadWriteUserDetails(String textBirthday, String textDiscount) {
        this.birthday = textBirthday;
        this.discount = textDiscount;
    }
}
英文:

I am creating a user profile but I do not know how can I get the child node and display it in the user profile, How can I reference this correctly? I would greatly appreciate any comments or suggestion, thank you in advance!!

USER PROFILE

private void showUserProfile( FirebaseUser firebaseUser) {

String userID = firebaseUser.getUid();

DatabaseReference referenceProfile = FirebaseDatabase.getInstance().getReference("Users"); 
// 👆
referenceProfile.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        ReadWriteUserDetails readUserDetail = snapshot.getValue(ReadWriteUserDetails.class);
        if(readUserDetail != null){
            fullname = firebaseUser.getDisplayName();
            email = firebaseUser.getEmail();
            doB = readUserDetail.birthday;
            discount = readUserDetail.discount;

            textViewFullName.setText(fullname);
            textViewEmail.setText(email);
            textViewDoB.setText(doB);
            textViewDiscount.setText(discount);
        }
    }

Firebase

如何获取正确的DatabaseReference

ReadWriteUserDetail class

public class ReadWriteUserDetails {
    public String birthday, discount;

    public ReadWriteUserDetails(){};

    public ReadWriteUserDetails(String textBirthday, String textDiscount){
        this.birthday = textBirthday;
        this.discount = textDiscount;
    }
}

答案1

得分: 0

你将用户配置文件存储在数据库的路径/Users/Customers下,因此可以设置对该父节点的引用如下:

DatabaseReference referenceProfile =
                         FirebaseDatabase.getInstance().getReference("Users/Customers");
英文:

You're storing your user profiles under path /Users/Customers in your database, so you can set up a reference to that parent node as:

DatabaseReference referenceProfile =
                         FirebaseDatabase.getInstance().getReference("Users/Customers"); 

huangapple
  • 本文由 发表于 2023年8月10日 09:40:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定