heisenberg3008
heisenberg3008

Reputation: 65

Retrieve real-time data from firebase for specific user

Answer to this has been posted without the PostIME error here : View PostIME 0,1 error while retrieving data from firebase

Thanks to @Alex Mamo and @Peter Haddad

Thanks @Akash Starvin Dsouza for trying.

My database :

enter image description here

I have given an ID to every user. And I want to retrieve the data of the specific user who is currently logged into the app. I am using this code wherein I am explicitly specifying the node under user details. What are the changes to be done?

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) {

                    }
                });
            }
        });

    }

Code used for SAVING data - email id, gender, height, name, weight:

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

Upvotes: 1

Views: 198

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

According to your comment:

I want the mail to be used as an identifier. How do I do that?

To achieve that, you should pass the email address into the reference when writing data to Firestore. So please change the following line of code:

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

To:

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

In this way, you'll not have those ascending numbers as identifiers but the email address. Another more appropriate approach would be to use as a unique identifier the uid that comes from the authentication process. The main reason is the user can change the email address over time while the uid will always be the same.

Upvotes: 1

try this

reference= FirebaseDatabase.getInstance().getReference().child("User Details").orderByChild("email").equalTo("[email protected]");

Here "email" should match database key value

orderByChild("email")

You should get email id of the when the user logs in, and pass it to

equalTo(strEmailId)

Sending data

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

Receiving data

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

Upvotes: 0

Related Questions