Bret
Bret

Reputation: 3

FireBase Android Need 1 value saved under a single user

I have the following data structure on firebase for the user MF0qeRA4p7djfjgXxqwFOck3m6p02. I want to get the value of item3 to populate a single field into the User interface on an Android App. I have been looking through samples on Stackoverflow, but all I have found are outdated and do not work with the current version of firebase. I'm new to firebase completely and this is my first app on android. I've got the oncreate user method to populate the users email address and add the 4 item fields, but retrieving the data I'm completely lost and I am not sure where to even begin.

-Users

---MF0qeRA4p7djfjgXxqwFOck3m6p02

------item1:"1"

------item2:"2"

------item3:"3"

------item4:"4"

Upvotes: 0

Views: 208

Answers (2)

Prince Bansal
Prince Bansal

Reputation: 1655

According to what I can identify is, you are facing problem retrieving data from this reference. Here is the code:

final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");

databaseReference.child("MF0qeRA4p7djfjgXxqwFOck3m6p02").addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            Map<String, Object> map=(Map<String, Object>)dataSnapshot.getValue();
                            String item3=(String)map.get("item3");
                            display(item3);
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });

Hope this helps.

Upvotes: 2

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

You can create a custom model and inside you can insert elements. Something like this:

public class Item {
    private List<Object> ojects;
}

There you can save instance of Item on database. In this case you have more controll. Other case is to use push() method, that will generate a new encoded key, something like this:

mDatabase.child("items").push().put(new Object());

Upvotes: 0

Related Questions