Reputation: 644
So I'm trying to create a listener that will advertise the activity when a new entry is added to my database in Firebase like this:
public void dataListener() {
Query lastQuery = mDatabase.child("id").orderByKey().limitToLast(1);
lastQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("ChildAdded",dataSnapshot.getChildren().toString());
getAddedUpdates(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("ChildError",databaseError.toString());
}
});
I'm calling this dataListeneronCreate
but nothing happens, and by that I mean, when I added a new entry on another phone this isn't triggered, even thought the data is being inserted (when i reload the app using a singleEeventListener in another method the data is received)
All fields are Text (Strings)
Upvotes: 0
Views: 710
Reputation: 80952
The location is wrong, you need to change this:
Query lastQuery = mDatabase.child("id").orderByKey().limitToLast(1);
into this:
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
Query lastQuery=ref.orderByKey().limitToLast(1);
the snapshot is at the root node then you will be able to order the keys and retrieve the last one.
The child("id")
does not have any key under it, example:
id
pushid
name: peter
The child'("id")
is equal to a randomid then you can do this:
Query lastQuery=ref.orderByChild("id").equalTo(key_here)
Upvotes: 1