BramH
BramH

Reputation: 221

Android Studio firebase get key of child with specific value

I have am developing an app in Android Studio and I am using FireBase to store data. Lets say I have a node that has multiple children, I would like to get the key of the child that has a specific value. For example;

enter image description here

I would like to read from the node 'languages', the key of the child with value 'English' so that I get 'eng'. Note that upfront I do not know that the key is 'eng'!

Someone knows a way to achieve this, without importing all the children?

Upvotes: 0

Views: 2461

Answers (1)

Levi Moreira
Levi Moreira

Reputation: 11995

Try something like this:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("languages");

        ref.orderByValue().equalTo("English").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snap : dataSnapshot.getChildren()) {
                    String value = snap.getValue(String.class);
                    String key = snap.getKey();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Upvotes: 3

Related Questions