There is an application in it posts. Likes, done as follows:
postReference = firebaseDatabase.getInstance().getReference().child("Posts");
holder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final DatabaseReference likeReference = postReference.child(listUserId).child("Likes");
likeReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(currentUserId)) {
likeReference.child(currentUserId).removeValue();
} else {
likeReference.child(currentUserId).setValue("Like");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
The problem is that after pressing the button, the record in the database begins to be added and deleted without stopping. How to fix it? My DB: