2017-09-03 48 views
0

鑑於以下DB計劃: enter image description here火力地堡不調用OnChildChanged活動

我要聽任何mHotZoneRecord變化。這樣做,我做了如下:

mDBRefUsers = mDatabase.getReference("https://stackoverflow.com/users/"); 
mDBRefUsers.child("mHotZoneRecord").addChildEventListener(new HotZoneNotification(this)); 

和其他地方的代碼中,實現HowZoneNotification

class HotZoneNotification extends TableSyncInfo 
     implements ChildEventListener { 
     OnChildChanged(); 
     OnChildAdded(); 
} 

的問題是我不是任何得到OnChildChanged事件。

我在做什麼錯了?

+0

'只有在'/ users'下的現有節點發生變化時,纔會調用'onChildChanged()'。 –

+0

你沒有實現這些方法嗎?也許我們可以幫助更多,如果你顯示'HotZoneNotification'的完整實現 –

+0

這些已經實現,但由我刪除,因爲在我看來這個例子並不重要,因爲回調本身沒有被調用,並且mDbLitute和mdbLongtitude做了改變 –

回答

0

您將用戶設置爲不在mHotZoneRecord上。要設置監聽器,您必須訪問'-Kt8Y ......',以便執行您想要的操作,然後執行這些更改。

mDBRefUsers = mDatabase.getReference("https://stackoverflow.com/users/"); 
mDBRefUsers.addListenerForSingleValueEvent(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
      // dataSnapshot .. hold list of users keys .. now you can hold it to set your listener 
      for(DataSnapshot dataSnapShot1 : dataSnapshot.getChildren()){ 
        mDBRefUsers.child(dataSnapShot1.getValue(String.class)) 
        .child("mHotZoneRecord") 
        .addChildEventListener(new HotZoneNotification(this)); 
        // this equals users.child("-Kt8YIIAEH6EKXIbh3YN").child("mHotZoneRecord").[YOU LINSTER] 

      } 
    } 

你做錯的是你告訴數據庫,有一個名爲「mHotZoneRecord」在用戶分支 這樣PIC enter image description here

因此,我所做的孩子,我在「用戶」分支進入然後我得到所有的密鑰「Kt8YIIAEH6EKXIbh3YN - Kt8A ....」然後我設置監聽器在「mHotZoneRecord」孩子包含在鍵中。

+0

感謝你的回答?但爲什麼使用SingleValueEvent而不是每次都會調用的常規valueEvent? –

+0

,因爲您只需訪問密鑰,因此您無需在密鑰上設置事件偵聽器。 –