skip to Main Content

I tried following various youtube videos for guides in order to display the current user information in user profile.However, I still failed on displaying those data. Those data are name, gmail and phone numbers.

My current java code on ProfileActivity


    FirebaseUser user;
    DatabaseReference reference;
    String userID;

    Button MainMenu,Logout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);

        user = FirebaseAuth.getInstance().getCurrentUser();
        reference = FirebaseDatabase.getInstance().getReference("users");
        userID = user.getUid();

        final TextView ViewName = (TextView) findViewById(R.id.Name);
        final TextView ViewEmail = (TextView)  findViewById(R.id.Email);
        final TextView ViewPhonenumber = (TextView)  findViewById(R.id.Phonenumber);

        reference.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                users userProfile = snapshot.getValue(users.class);

                if(userProfile != null){
                    String name = userProfile.Name;
                    String email = userProfile.Email;
                    String phonenumber = userProfile.Phonenumber;


                    ViewName.setText(name);
                    ViewEmail.setText(email);
                    ViewPhonenumber.setText(phonenumber);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(ProfileActivity.this,"Something wrong happened",Toast.LENGTH_LONG).show();
            }
        });

My current file for users.java


    public String Name;
    public String Email;
    public String Phonenumber;

    public users(){

    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getEmail() {
        return Email;
    }

    public void setEmail(String email) {
        Email = email;
    }

    public String getPhonenumber() {
        return Phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        Phonenumber = phonenumber;
    }
}

My realtime database in Firebase
enter image description here

UPDATED with Register.java class so you guys might get clear view on how my code looks like


    EditText mFullName,mEmail,mPassword,mPhone;
    Button mRegisterButton;
    TextView mLoginButton;
    FirebaseAuth fAuth;
    ProgressBar progressBar;
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference root = db.getReference().child("users");

    users users;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        mFullName           = findViewById(R.id.Name);
        mEmail              = findViewById(R.id.Email);
        mPassword           = findViewById(R.id.Password);
        mPhone              = findViewById(R.id.Phonenumber);
        mRegisterButton     = findViewById(R.id.registerbutton);
        mLoginButton        = findViewById(R.id.loginpage);


        fAuth = FirebaseAuth.getInstance();
        progressBar = findViewById(R.id.progressBar);

        if(fAuth.getCurrentUser() != null)
        {
            startActivity(new Intent(getApplicationContext(),MainActivity.class));

        }


        mRegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = mEmail.getText().toString().trim();
                String password = mPassword.getText().toString().trim();
                String name = mFullName.getText().toString();
                String number =mPhone.getText().toString();

                HashMap<String , String> userMap = new HashMap<>();

                userMap.put("name",name);
                userMap.put("email",email);
                userMap.put("phone",number);



                if(TextUtils.isEmpty(email))
                {
                    mEmail.setError("Email is required!");
                    return;
                }

                if(TextUtils.isEmpty(password))
                {
                    mPassword.setError("Password is required!");
                    return;
                }

                if(password.length() < 6)
                {
                    mPassword.setError(("Password must be longer than 6"));
                    return;
                }

                progressBar.setVisibility(View.VISIBLE);

                fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if(task.isSuccessful()){
                            root.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(userMap);
                            Toast.makeText(Register.this,"User Created",Toast.LENGTH_LONG).show();
                            startActivity(new Intent(getApplicationContext(),MainActivity.class));

                        }else{
                            Toast.makeText(Register.this,"Error" + task.getException().getMessage(),Toast.LENGTH_SHORT).show();
                            progressBar.setVisibility(View.GONE);

                        }
                    }
                });
            }
        });```

2

Answers


  1. You’re getting no data from the database because the names of the properties inside the users class are different than the ones in the database. See Name (capital N) in the class vs. name (lower case letter n) in the database?

    To solve this, you should simply change the class to look like this:

    class users
        private String name;
        private String email;
        private String phonenumber;
    
        public users(){
    
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPhonenumber() {
            return phonenumber;
        }
    
        public void setPhonenumber(String phonenumber) {
            this.phonenumber = phonenumber;
        }
    }
    

    Please also note that the fields now are set private instead of public. Besides that, to refer to the class members, you have to use this, which is an instance of the class.


    P.S. Also remember that in your database you’re using pushed IDs and not UIDs that are coming from the authentication operation.

    Login or Signup to reply.
  2. Follow these steps

    1. Change your User class fields to :

      public String name;
      public String email;
      public String phone;

      also rename getter and setters

    2. Log error in onCancelled() if this does not work

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search