2

I have a real-time firebase database that stores User address. I want to get the values and assign them to textviews in android layout. I have tried access the data like this:

String city = (mDatabase.getDatabase().getReference().child(user_data.getString("uidkey", null)).child("city")).toString();
        _cityText.setText(city);

But that gives me only the firebase URL of the the street and not the actual value. How can I go about retrieving this data?

This is how the database looks like.

public void onStart(){
        super.onStart();
        final SharedPreferences user_data = getSharedPreferences("PESASEND_PREFS", Context.MODE_PRIVATE);

        mDatabase.child(user_data.getString("uidkey", null)).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Address address = dataSnapshot.getValue(Address.class);
              //  address.getCity();

                _state_province_Text.setText(address.getStreet());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

.

public class Address {

    public String street;
    public String city;
    public String state;
    public String zip_postal_code;
    public String country;

    public Address (){};

    public Address(String street, String city, String state, String zip_postal_code, String country ){
        this.street = street;
        this.city = city;
        this.state = state;
        this.zip_postal_code = zip_postal_code;
        this.country = country;
    }

    public String getStreet (){return street;}
    public String getCity (){return city;}
    public String getState (){return state;}
    public String getZipPostalCode (){return zip_postal_code;}
    public String getCountry (){return country;}

}
Charuක
  • 12,953
  • 5
  • 50
  • 88
mpanga
  • 107
  • 3
  • 11
  • Possible duplicate of [Firebase retrieve child Android](http://stackoverflow.com/questions/27848313/firebase-retrieve-child-android) – JP Ventura Dec 28 '16 at 10:13

1 Answers1

0

The recommended way to get data on an entry in firebase is to create class representing the object you want to pull from the DB, making sure all of the members of that class match exactly the keys of the data you wish to pull (e.g. you would have an object with 5 strings, city, country, state, street, zip_postal_code) and make sure all of these objects have setters and getters (just auto generate these)

Then, you can add a listener for your database reference like so:

mDatabase.child("Addresses").child(user_data.getString("uidkey", null)).addListenerForSingleValueEvent(
        new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // Get the address object
                Address address = dataSnapshot.getValue(Address.class);
                // get the string here, using address.getCity() or whatever you called the getter

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

You can also add new address objects to your firebase using the same address class.

Additionally, if you are populating a list view, this can be much simpler and cleaner if you use firebaseListAdapter

Dportology
  • 836
  • 1
  • 9
  • 32
  • This is what I have written: I have written that same code and put it in the onStart(); I use String street = addess.getStreet(). This however causes the app to crash due to a null exception. – mpanga Sep 13 '16 at 15:11
  • thats the address class that I am using and the one above is how I am getting it. – mpanga Sep 13 '16 at 15:15
  • See edits to my answer. You need to add "Addresses" as a child of your DB reference before the user ID child. – Dportology Sep 13 '16 at 15:19
  • You're welcome man. Feel free to select my answer as correct! – Dportology Sep 13 '16 at 15:31