At this line of my code in my OnDataChange()
method in the ValueEvenListener
:
int latest = dataSnapshot.getValue(Integer.class);
Your ValueEventListener
is attached to the whole database.
// This line gets a reference to the whole database
final DatabaseReference database = FirebaseDatabase.getInstance().getReference();
// This line creates a child DatabaseReference, but you don't assign
// the child to a variable
database.child("Campaigns").child(key).child("count");
// This line adds a ValueEventListener to the whole database
database.addValueEventListener(new ValueEventListener() {
What you want instead is this:
final DatabaseReference database = FirebaseDatabase.getInstance().getReference()
final DatabaseReference countRef = database.child("Campaigns").child(key).child("count")
countRef.addValueEventListener(new ValueEventListener() {
// ...
});
You can see that in the latter example the ValueEventListener is attached to the child reference, not to the root.