2017-04-04 162 views
-2

我的火力數據庫的結構是這樣的:檢索從火力地堡Android Studio中

-Details 
    -BNFJ3491 
     -Answer: "abc" 
     -Name: "ABC" 
     -Password:"cde" 
     -Security Question: "rgrg" 
     -User Type: "Student" 
    -CBTV15873 
     -Answer: "abc" 
     -Name: "ABC" 
     -Password:"cde" 
     -Security Question: "rgrg" 
     -User Type: "Student" 
-Notices 
    -B tech placement 
     -Department: "Urja Mandir" 
     -Matter: "Hello welcome to btech placement cell" 

Firebase ref=new Firebase("https://linkb-873e1.firebaseio.com/Details"); 
ref.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        //get all the children 

        for (DataSnapshot child : dataSnapshot.getChildren()) { 
         for (DataSnapshot child1 : child.getChildren()) { 


         } 
        } 
       } 


       @Override 
       public void onCancelled(FirebaseError firebaseError) { 
        System.out.println("The read failed: " + firebaseError.getMessage()); 
       } 

      }); 

我無法弄清楚如何爲一個特定ID獲取密碼(如用於BNFJ3491 )datasnapshot將得到我所有的數據,但我如何檢索有關特定ID的信息。

回答

1

請使用此代碼:

Firebase ref=new Firebase("https://linkb-873e1.firebaseio.com/Details"); 
ref.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       String password = dataSnapshot.child(-BNFJ3491).child("Password").getValue(); 
      } 


      @Override 
      public void onCancelled(FirebaseError firebaseError) { 
       System.out.println("The read failed: " + firebaseError.getMessage()); 
      } 

     }); 
+0

謝謝你,先生,這段代碼的工作! – sri

0

您可以使用此代碼

ref.child(userId).addValueEventListener(new ValueEventListener() { 
@Override 
public void onDataChange(DataSnapshot dataSnapshot) { 
    String password = dataSnapshot.child("Password").getValue(String.class); 
} 

@Override 
public void onCancelled(DatabaseError databaseError) {} 
}); 
+0

謝謝你的幫助! – sri

+0

不客氣! – mohnage7