当一个孩子被添加到我的firebase中时,它会再次记录我的firebase中的所有Annotation_City。如何让它只打印添加的新Annotation_City?
public void AllLocationMarkers() {
grabMarkersFromDb = new Firebase("***");
grabMarkersFromDb.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot locationSnapshot : dataSnapshot.getChildren()) {
String city_Name = (String) locationSnapshot.child("Annotation_City").getValue();
Log.d("City updated", "city: " + city_Name);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
答案 0 :(得分:0)
grabMarkersFromDb.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//value add then it will call
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//value change
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// value remove
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
使用 addChildEventListener 。它会一个接一个的价值。如果值为add,则其方法 onChildAdded 将调用。