2017-01-16 65 views
0

enter image description here如何检索使用迭代器

如何获得使用迭代此JSON结构的关键在火力地堡的关键? 逻辑是,如果用户是哈利·安德森,我想要搜索person0价值高达person6,如果该值被发现我想要得到它的关键在这种情况下是-KaSeO5n2xCl2sEnDpSi。我不知道这是否会使用迭代器来实现,但是如果有其他方法可以做到这一点,请确实建议。谢谢。

+0

参考链接显式方法: - http://stackoverflow.com/questions/37094631/ get-the-pushing-id-for-specific-value-in-firebase-android http://stackoverflow.com/questions/38232140/how-to-get-the-key-from-the-value-in-firebase – Nainal

+0

感谢您的回复,但我无法通过链接找到答案。 @Nainal –

回答

0

您可以尝试

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 
    }}); 

我不认为这是做

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("[email protected]@_Key::>", child.getKey()); 
         keyList.add(child.getKey()); 
       } 
       // your all keys are store in KeyList ArrayList 
      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 
       Log.e("[email protected]@@_ERROR_>>", "onCancelled", firebaseError.toException()); 
      } 
     });