从Firebase检索特定用户的实时数据

时间:2020-03-29 18:07:56

标签: android firebase firebase-realtime-database

有关此问题的答案已发布,此处没有PostIME错误:View PostIME 0,1 error while retrieving data from firebase

感谢@Alex Mamo和@Peter Haddad

感谢@Akash Starvin Dsouza的尝试。

我的数据库:

enter image description here

我给每个用户一个ID。我想检索当前登录到该应用程序的特定用户的数据。 我正在使用此代码,其中我在用户详细信息下明确指定了节点。有哪些更改要做?

showb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reference= FirebaseDatabase.getInstance().getReference().child("User Details").child("1");
                reference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        String namep=dataSnapshot.child("name").getValue().toString();
                        String heightp=dataSnapshot.child("height").getValue().toString();
                        String weightp=dataSnapshot.child("weight").getValue().toString();
                        String genderp=dataSnapshot.child("gender").getValue().toString();
                        String emailp=dataSnapshot.child("email").getValue().toString();
                        name.setText(namep);
                        height.setText(heightp);
                        weight.setText(weightp);
                        gender.setText(genderp);
                        mail.setText(emailp);
                    }

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

                    }
                });
            }
        });

    }

用于保存数据的代码-电子邮件ID,性别,身高,姓名,体重:

msave=(Button)findViewById(R.id.save);

        msave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //n
                String uName= uname.getText().toString().trim();
                Float uHeight = Float.parseFloat(uheight.getText().toString().trim());
                Float uWeight = Float.parseFloat(uweight.getText().toString().trim());
                String uGender= ugender.getText().toString().trim();
                String uEmail=  msuser.getText().toString().trim();

                ud.setName(uName);
                ud.setHeight(uHeight);
                ud.setWeight(uWeight);
                ud.setGender(uGender);
                ud.setEmail(uEmail);
                reference.child(String.valueOf(maxid+1)).setValue(ud);
                //n

                final String email= msuser.getText().toString();

                String pwd=mspass.getText().toString();
                String cpwd=msconfpass.getText().toString();

                if(email.isEmpty()){
                    msuser.setError("Please enter email id");
                    msuser.requestFocus();
                }

                else if(pwd.isEmpty()){
                    mspass.setError("Please enter the password");
                    mspass.requestFocus();
                }
                else if(cpwd.isEmpty()){
                    msconfpass.setError("Please enter password again");
                    msconfpass.requestFocus();
                }
                else if(email.isEmpty() && pwd.isEmpty() && cpwd.isEmpty()){
                    Toast.makeText(Signup.this,"Fields are empty",Toast.LENGTH_SHORT).show();
                }
                else if(!cpwd.equals(pwd)){
                    Toast.makeText(Signup.this,"Please enter same password",Toast.LENGTH_SHORT).show();
                }
                else if(!(email.isEmpty() && pwd.isEmpty() && cpwd.isEmpty())){

                    mfirebase.createUserWithEmailAndPassword(email,pwd).addOnCompleteListener(Signup.this, new OnCompleteListener<com.google.firebase.auth.AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<com.google.firebase.auth.AuthResult> task) {
                            if(!task.isSuccessful()){
                                Toast.makeText(Signup.this,"Signup Unsuccessful",Toast.LENGTH_SHORT).show();
                            }
                            else
                            {

                                Toast.makeText(Signup.this,"Account Created Successfully and You're now logged in",Toast.LENGTH_SHORT).show();
                                finish();
                                startActivity(new Intent(Signup.this,Login.class));
                                //Toast.makeText(Signup.this,"Account Created Successfully",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
                else{
                    Toast.makeText(Signup.this,"Error",Toast.LENGTH_SHORT).show();
                }
            }
        });

2 个答案:

答案 0 :(得分:1)

根据您的评论:

我希望将邮件用作标识符。我该怎么办?

为此,在将数据写入Firestore时,应将电子邮件地址传递到引用中。因此,请更改以下代码行:

reference.child(String.valueOf(maxid+1)).setValue(ud);

收件人:

reference.child(uEmail).setValue(ud);

通过这种方式,您不会将那些升序数字用作标识符,而会将其作为电子邮件地址。另一种更合适的方法是使用来自身份验证过程的uid作为唯一标识符。主要原因是用户可以随时更改电子邮件地址,而uid始终是相同的。

答案 1 :(得分:0)

尝试

reference= FirebaseDatabase.getInstance().getReference().child("User Details").orderByChild("email").equalTo("yyy@gmail.com");

此处的“电子邮件”应与数据库键值匹配

orderByChild("email")

您应该在用户登录时获得的电子邮件ID,并将其传递给

equalTo(strEmailId)

发送数据

Intent intoHome=new Intent(Login.this,HomeActivity.class);
intoHome.putExtra("email_key", email);
startActivity(intoHome);

接收数据

Bundle extras = getIntent().getExtras();
strRecivedEmail = extras.getString("email_key");