1

I am storing user details 'firstname' and 'lastname' in UserNode. But when i want to retrieve that details then no data is being retrieved. I tried almost all solutions on the internet but nothing solved my problem. Here is my code for retrieving data of the current user:

FirebaseUser userr = FirebaseAuth.getInstance().getCurrentUser();
    if (userr != null) {
        String name = userr.getDisplayName();
        Log.e("value", name);

    }

but it says "println needs a message"

I also tried with this but nothing happened:

DatabaseReference DataRef;
    DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode");

    DataRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String acctname = (String)dataSnapshot.child("firstname").getValue();
            Log.e("name", acctname);


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Here is the structure of database]1

Please help me I am stuck with it

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • yes sir @Tomas wait a second i am going to edit the post –  Jun 25 '17 at 19:35
  • @Tomas I have updated the code kindly check –  Jun 25 '17 at 19:37
  • "I am storing user details 'firstname' and 'lastname' in UserNode" - The code you shared doesn't show that. Please update your question to include the [minimum, complete code that reproduces the problem](http://stackoverflow.com/help/mcve) (read the link - while long, it is incredibly useful). Also update your question to include the JSON as text, instead of a link to an image. You can get this by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jun 25 '17 at 20:30

3 Answers3

0

Please, read how to retrieve data from firebase. I think you have a problem because you don't have Class Model.

Your steps:

  1. Create model UserModel with firstname and lastname field
  2. Use listener (example from docs):

    // Attach a listener to read the data at our posts reference
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Post post = dataSnapshot.getValue(Post.class);
            System.out.println(post);
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            System.out.println("The read failed: " + databaseError.getCode());
        }
    });
    
  3. See other answers: How to retrieve data from one single userID Firebase Android and retrieving data from firebase android

BhalchandraSW
  • 714
  • 5
  • 13
Tomas
  • 1,567
  • 3
  • 21
  • 38
  • a Java class to represent the data is recommended, but not necessary. In Arslan's code `dataSnapshot.child("firstname").getValue()` will get the `firstname` property of the snapshot and then return the value as its native underlying type (likely a `String` given the property name). – Frank van Puffelen Jun 25 '17 at 20:28
0

Using FirebaseUser:

FirebaseUser implements UserInfo and in UserInfo's getDisplayName() documentation says

Returns the user's display name, if available.

So, it is possible that FirebaseUser.getDisplayName() return null when display name is not set. In that case Log.e() receives null as message and therefore prints println needs a message

Using your own structure:

Instead of using type conversion use getValue(Class<T>) like so:

String acctname = dataSnapshot.child("firstname").getValue(String.class);
BhalchandraSW
  • 714
  • 5
  • 13
0

You're reading a collection of user with a ValueEventListener. As the [Firebase documentation for reading lists with a value event](Listen for value events) explains:

While using a ChildEventListener is the recommended way to read lists of data, there are situations where attaching a ValueEventListener to a list reference is useful.

Attaching a ValueEventListener to a list of data will return the entire list of data as a single DataSnapshot, which you can then loop over to access individual children.

Even when there is only a single [child node], the snapshot is still a list; it just contains a single item. To access the item, you need to loop over the result.

So in your code:

DatabaseReference DataRef;
DataRef = FirebaseDatabase.getInstance().getReference().child("UserNode");

DataRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
            String acctname = (String)childSnapshot.child("firstname").getValue();
            Log.i("name", acctname);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});
Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thankyou for answeing, and sorry for late response, Sir getChildren() is giving error and showing red line and it says "non-static method getChildren cannot be referenced from a static context. What to do in this case? –  Jun 26 '17 at 18:31
  • Oops... that was a typo: `DataSnapshot.getChildren()` -> `dataSnapshot.getChildren()`. Fixed. – Frank van Puffelen Jun 26 '17 at 19:38
  • Still the same issue, :( println needs a message –  Jun 26 '17 at 19:44
  • There was another type. Sorry about that, sometimes the copy/paste/modify goes awry. But I'm not sure what you mean about println, since there is no such call in my aswer. – Frank van Puffelen Jun 26 '17 at 19:49
  • I have been trying to solve the issue from 3 days but no one gives me correct answer. But you are genious. Thankyou it works thanks a lot for help :) Just one last thing, it shows all the firstnames of the UserNode data. How to show only current user firstname who is logged in –  Jun 26 '17 at 19:57