Home > Blockchain >  How to read and display from realtime database [Firebase]?
How to read and display from realtime database [Firebase]?

Time:04-18

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related