0

I am trying to retrieve data from my firebase database and print a statement in a text field but I keep getting null values. I'm using the userid to try and get to the child node that I want. My textfield keeps saying "Welcome null logged-in as null" My Firebase Database

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        final String uid;
        FirebaseUser user;


        user = FirebaseAuth.getInstance().getCurrentUser();
        uid = user.getUid();

        DatabaseReference reference = 
      FirebaseDatabase.getInstance().getReference("User");


         reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                fName = dataSnapshot.child(uid).child("fName").getValue(String.class);
                role = dataSnapshot.child(uid).child("role").getValue(String.class);

            }
            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        welcomeText = (TextView) findViewById(R.id.welcomeText);
        welcomeText.setText("Welcome " + fName + "! You are logged-in as " + role);
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
JR1232
  • 7
  • 4
  • Have you debugged to see exactly what's in `dataSnapshot`? Is your callback even being called? – Jon Skeet Oct 20 '19 at 06:51
  • If you are getting correct data in DataSnapshot then move this line welcomeText.setText("Welcome " + fName + "! You are logged-in as " + role) inside onDataChanged(). – Kishan Maurya Oct 20 '19 at 07:12
  • 1
    Most likely you're being affected by the asynchronous nature of loading data from Firebase, and your `welcomeText.setText(...)` executes before the `onDataChange` has been called. See my answer here for more details and examples: https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Oct 20 '19 at 14:39

2 Answers2

1

Move the following lines to the onDataChange() callback method.

welcomeText = (TextView) findViewById(R.id.welcomeText);
welcomeText.setText("Welcome " + fName + "! You are logged-in as " + role);
Rohit Rawat
  • 449
  • 1
  • 4
  • 14
1

You should pass your userid as child to get data for only the logged in user. Currently you loop through all the users which gives you wrong data at the end. Try like below:

DatabaseReference reference = 
      FirebaseDatabase.getInstance().getReference("User").child(uid);

instead of

DatabaseReference reference = 
      FirebaseDatabase.getInstance().getReference("User");

Beside this, Currently you are setting the initial value of fName and role which is null and never update the value with your user detail which you get inside onDataChange. So, update your TextView inside onDataChange.

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    fName = dataSnapshot.child("fName").getValue(String.class);
    role = dataSnapshot.child("role").getValue(String.class);

    welcomeText.setText("Welcome " + fName + "! You are logged-in as " + role);
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46