无法从Android Studio中的Firebase实时数据库检索数据

时间:2020-04-30 03:58:21

标签: android firebase firebase-realtime-database

我是Firebase和android studio的新手。我正在一个项目中,我必须在其中获取当前用户的详细信息并显示名称。即使我已经登录,但在下面附加了所有内容,也会抛出“用户不存在”吐司。也许我犯了一些错误,请告诉我。 Screenshot of FIrebase Relaltiem Database:

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

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));
            }

    }
}

我的HelperClass

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;
    }


}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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) {

}
});