【问题标题】:Not able to retrieve data from Firebase real time database in Android studio无法从 Android Studio 中的 Firebase 实时数据库中检索数据
【发布时间】:2020-04-30 21:04:27
【问题描述】:

我是 firebase 和 android 工作室的新手。我正在做一个项目,我必须获取当前用户的详细信息并显示名称。即使我已登录,它也会抛出“用户不存在”吐司我正在附上下面的所有内容。也许我犯了一些错误,请告诉我。

获取当前用户详细信息的代码

public class Succesfully_sign_up extends AppCompatActivity {

    DatabaseReference reff;
    TextView name;
    Button signout;
    FirebaseUser curr_user;
    String curr_user_str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_succesfully_sign_up);
        name = findViewById(R.id.name);
         curr_user=FirebaseAuth.getInstance().getCurrentUser();
        if(curr_user!=null) 
       {
             curr_user_str = curr_user.getUid();
            reff = FirebaseDatabase.getInstance().getReference("userdetails");
            reff.child(curr_user_str).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    HelperClass data = dataSnapshot.getValue(HelperClass.class);
                    if (data != null) {
                        name.setText(data.getFullname_hc());
                    } else {
                        Toast.makeText(Succesfully_sign_up.this, "User does't exist", Toast.LENGTH_SHORT).show();
                    }

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
        }
         else
            {
                startActivity(new Intent(Succesfully_sign_up.this,student_signup.class));
            }

    }
}

我的助手类

public class HelperClass {
    String fullname_hc,amizone_id_hc,mobile_num_hc,email_id_hc;
    public HelperClass()
    {

    }

    public HelperClass(String fullname_hc, String amizone_id_hc, String mobile_num_hc, String email_id_hc) {
        this.fullname_hc = fullname_hc;
        this.amizone_id_hc = amizone_id_hc;
        this.mobile_num_hc = mobile_num_hc;
        this.email_id_hc = email_id_hc;
    }

    public String getFullname_hc() {
        return fullname_hc;
    }

    public void setFullname_hc(String fullname_hc) {
        this.fullname_hc = fullname_hc;
    }

    public String getAmizone_id_hc() {
        return amizone_id_hc;
    }

    public void setAmizone_id_hc(String amizone_id_hc) {
        this.amizone_id_hc = amizone_id_hc;
    }

    public String getMobile_num_hc() {
        return mobile_num_hc;
    }

    public void setMobile_num_hc(String mobile_num_hc) {
        this.mobile_num_hc = mobile_num_hc;
    }

    public String getEmail_id_hc() {
        return email_id_hc;
    }

    public void setEmail_id_hc(String email_id_hc) {
        this.email_id_hc = email_id_hc;
    }


}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    我认为您已经手动创建了数据库,您的用户 ID 8448.... 不是由 Firebase android 生成的,因此显示该错误。如果没有,请分享上述数据库结构的创建代码。

    【讨论】:

    • 是的,这就是问题所在!我通过将数据库的主键设置为 Uid 来修复它。但是为什么在我将主键设置为电话号码之前 getUid() 不返回主键?我认为它会返回我将设置的主键。
    • getUid() 将始终返回由 Firebase 身份验证系统创建的 ID,ID 是否用作主键都没有关系,因此 getUid() 永远不会返回您的手动创建主键。如果您有任何疑问,请告诉我谢谢
    • 如果您对我的回答感到满意,请点赞。谢谢
    【解决方案2】:

    如何循环遍历孩子并访问数据:

    //the reference
    reff = FirebaseDatabase.getInstance().getReference("userdetails");
    
    
    //the reading
    
     reff.child.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    
     //loop through every possible child under "userdetails"
    for(DataSnapshot ds : dataSnapshot.getChildren()){
    
    String fullName = ds.child("fullname_hc").getValue(String.class);
    name.setText(fullName);
    
    }
    
    
    
    }
    
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    
    }
    });
    

    【讨论】:

    • 谢谢!但是我通过将数据库的主键设置为 Uid 来修复它。但是为什么在我将主键设置为电话号码之前 getUid() 不返回主键?我认为它会返回我将设置的主键。
    猜你喜欢
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2019-08-29
    • 1970-01-01
    • 2019-04-25
    相关资源
    最近更新 更多