Guilherme Gomes Zucco
Guilherme Gomes Zucco

Reputation: 35

Firebase to Android

Tree of Firebase

I would like to put this fields "age, email, firstname" [you can see clicking in the Tree of Firebase above] in three Strings "userAge, userEmail, userName" and put then in a text view, when I use this code to get the name

     FirebaseDatabase database = FirebaseDatabase.getInstance();
     DatabaseReference ref = database.getReference();
     String userId;
     String userName;
    //.
    //.
    //.
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user == null) {
        finish();
    } else {
        userId = user.getUid();
        ref.child("users").child(userId).child("firstname").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                userName = dataSnapshot.getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
      textview.setText(userName);

I can get the name and put in the String userName, and show it in the text view but when I try tpo get the 3 fields with this code:

     FirebaseDatabase database = FirebaseDatabase.getInstance();
     DatabaseReference ref = database.getReference();
     String userId, UserName, userEmail, userAge
    //.
    //.
    //.     
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user == null) {
        finish();
    } else {
        userId = user.getUid();
        ref.child("users").child(userId).child("firstname").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                userName = dataSnapshot.getValue(String.class);
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }           
        });
        ref.child("users").child(userId).child("email").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                userEmail = dataSnapshot.getValue(String.class);
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        ref.child("users").child(userId).child("age").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                userAge = dataSnapshot.getValue(String.class);
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
           textview.setText(userName + ", " + userAge + "Years - " + useremail); 

The App doesn't get the fields and closes, have another way to do this?

Upvotes: 0

Views: 61

Answers (2)

nhoxbypass
nhoxbypass

Reputation: 10152

Use Integer.class instead of String.class for I saw your data on Firebase is NOT in a quote so it should be retrieve in Integer format.

ref.child("users").child(userId).child("age").addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
           userAge = dataSnapshot.getValue(Integer.class);
     }
     @Override
     public void onCancelled(DatabaseError databaseError) {
     }
}

Upvotes: 2

Bqin1
Bqin1

Reputation: 507

Actually your issue might be because age is an integer and you are trying to retrieve it as a string. Can you try commenting out age part of the code and see if it works?

Upvotes: 1

Related Questions