0

enter image description here

How to get the key in this json structure using Iterator? The logic would be, if the user is Harry Anderson, I want to search the value from person0 up to person6 and if the value is found i want to get its key which in this case is -KaSeO5n2xCl2sEnDpSi. I don't know if this will be achieved using Iterator, but if there are other ways to do this, please do suggest. Thanks.

elbert rivas
  • 1,464
  • 1
  • 17
  • 15
  • Refer the links:- http://stackoverflow.com/questions/37094631/get-the-pushed-id-for-specific-value-in-firebase-android http://stackoverflow.com/questions/38232140/how-to-get-the-key-from-the-value-in-firebase – Nainal Jan 16 '17 at 10:28
  • Thank you for the response but I could not find answers with the link. @Nainal – elbert rivas Jan 16 '17 at 10:36

2 Answers2

2
 ref.child("chat/chat-participants").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                ArrayList<String> keyList = new ArrayList<>();
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                    Log.e("!_@@_Key::>", child.getKey());
                        keyList.add( child.getKey());
                }
              // your all keys are store in KeyList ArrayList
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                Log.e("!_@@@_ERROR_>>", "onCancelled", firebaseError.toException());
            }
        });
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
0

You can try

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference chatRef = database.getReference("chat").child("chat-participants");

chatRef.addListenerForSingleValueEvent(
                    new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
    //do stuff iteration
    }});

I don't think there is an explicit method to do it