通過將異步偵聽器附加到FirebaseDatabase引用來檢索Firebase數據。對於數據的初始狀態,監聽器被觸發一次,並且在數據發生任何變化時再次觸發監聽器。
private DatabaseReference mDatabase;
mDatabase = FirebaseDatabase.getInstance().getReference();
要添加的值事件偵聽器,使用addValueEventListener()或addListenerForSingleValueEvent()方法。要添加子事件偵聽器,請使用addChildEventListener()方法。
可以使用onDataChange()方法讀取給定路徑上內容的靜態快照,因爲它們在事件發生時存在。該方法在監聽器連接時觸發一次,並且每次數據(包括子節點)都會更改。事件回調會傳遞包含該位置所有數據的快照,包括子數據。如果沒有數據,則返回的快照爲空。
實施例: -
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Post post = dataSnapshot.getValue(Post.class);
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
// ...
}
};
mPostReference.addValueEventListener(postListener);
兒童事件響應於發生從操作的節點的子節點的特定操作觸發例如一個新的孩子通過push()方法增加或一個孩子是通過updateChildren()方法更新。它們中的每一個都可以用於監聽數據庫中特定節點的更改。
例子: -
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildAdded:" + dataSnapshot.getKey());
// A new comment has been added, add it to the displayed list
Comment comment = dataSnapshot.getValue(Comment.class);
// ...
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildChanged:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so displayed the changed comment.
Comment newComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
// ...
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
Log.d(TAG, "onChildRemoved:" + dataSnapshot.getKey());
// A comment has changed, use the key to determine if we are displaying this
// comment and if so remove it.
String commentKey = dataSnapshot.getKey();
// ...
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String previousChildName) {
Log.d(TAG, "onChildMoved:" + dataSnapshot.getKey());
// A comment has changed position, use the key to determine if we are
// displaying this comment and if so move it.
Comment movedComment = dataSnapshot.getValue(Comment.class);
String commentKey = dataSnapshot.getKey();
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "postComments:onCancelled", databaseError.toException());
Toast.makeText(mContext, "Failed to load comments.",
Toast.LENGTH_SHORT).show();
}
};
ref.addChildEventListener(childEventListener);
在某些情況下,你可能需要一個回調被調用一次,然後立即刪除,例如初始化,你不要指望改變UI元素時。您可以使用addListenerForSingleValueEvent()方法來簡化此方案:它觸發一次,然後不再觸發。
這對於只需要加載一次而且預計不會頻繁更改或需要主動偵聽的數據很有用。例如,
例子: -
final String userId = getUid();
mDatabase.child("users").child(userId).addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get user value
User user = dataSnapshot.getValue(User.class);
// ...
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "getUser:onCancelled", databaseError.toException());
}
});
顯示代碼,我們不能幫你沒有它 –
@DimaRostopira我添加的代碼。 – Michal