DonDave
DonDave

Reputation: 63

Android Firebase: Retrieve ID

How to get the ID of the item "001" in the firebase database. I have made this database manually

 "movies" : {
    "action" : {
      "001" : {
                "name" :    "Firebase",
                "director": "Google"  
              }
               }
            }

Upvotes: 0

Views: 65

Answers (1)

vivek
vivek

Reputation: 36

firebase store data in json format which is represented as key-pair value.

So if you want to retrive item "001" which represents a key in your data structure.

To get "001" you can use Firebase addListenerForSingleValueEvent .

DatabaseReference mDatabaseReference =FirebaseDatabase.getInstance().getReference().child("movies").child("action");
mDatabaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.getValue() != null) {
                    HashMap mapRecord = (HashMap) dataSnapshot.getValue();
                    Iterator listKey = mapRecord.keySet().iterator();
                    while (listKey.hasNext()) {
                        String id = listKey.next().toString();
                   //you get you key here    
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Upvotes: 2

Related Questions