2015-07-28 27 views
1

我嘗試使用Firebase加入三個數據。我想檢索school name查詢到classes只知道防護密鑰的數據是g1。我知道它可以在兩個不同的調用中完成,但我只是在一次調用服務器的過程中嘗試完成。Firebase在Android中加入數據

有點可能嗎?

學校

schools : 
    schoolKey1 : 
    name: School One 
    schoolKey2 : 
    name: School Two 

classes : 
    someKey1 : 
    name: Class One 
    school : schoolKey1 
    guard : g1 

衛隊

guards : 
    g1 : 
    name: Pipoy Pat 

回答

3

這可能是太晚了,但你會需要遵循文檔here

類似:

var ref = new Firebase("https://myApp.firebaseio.com/"); 

    // fetch classes 
    ref.child("classes").addChildEventListener(new ChildEventListener() { 
     @Override 
     public void onChildAdded(DataSnapshot snapshot, String previousChildKey) { 
     //get school key 
     String schoolKey = snapshot.getKey(); 

     //get school name from key 
     ref.child("schools/" + schoolKey + "/name").addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot snapshot) { 
      System.out.println("The school name is " + snapshot.getValue()); 
      } 

      @Override 
      public void onCancelled(FirebaseError firebaseError) { 
      // ignore for now 
      } 
     }); 
     } 
    }); 
+0

這工作,但它不是實時的 - 沒有檢測到變化。你知道更好的解決方案嗎? –

+0

幫助我很多謝謝 –