Skip to content Skip to sidebar Skip to footer

Not Able To Retrieve Data Field From Firebase Database

I have been trying to retrieve data fields from my firebase database for the past 5 days without any success. This is what my database looks like: Code for fetching data: private

Solution 1:

dataSnapshot.getValue(Class) will only load and set values into public fields. I see you already have a default constructor.

Make name field in UserInformation class public. Also make sure the snapshot you are calling getValue on is a valid JSON representation of UserInformation class and has an exact same "name" field (it must exactly match the name field in class).

Please note: addValueEventListener adds a listener, which will be called when data is available.

Please note that the data is available after onDataChange has been called. That's why you should set your text right after you get the data you need from the DataSnapshot. Like that:

privatevoidalterTextView(final String id) {
            if(id!=null) {
            mDatabase.child("Users").child(id).addValueEventListener(newValueEventListener() {
                @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                    UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
                    String name = (String) userInformation.getName();
                    Log.d("Main Activity",name);
                    runOnUiThread(newRunnable() {
                        @Overridepublicvoidrun(){
                            mWelcomeUserMessage.setText("Welcome, "+name); 
                        }
                    });
                }

                @OverridepublicvoidonCancelled(DatabaseError databaseError) {

                }
            });

        }
    }

Post a Comment for "Not Able To Retrieve Data Field From Firebase Database"