mMessages
is a method. I have an SQLite table, and every time a new message is received I want to add +1 to a column. That's what the mMessages()
method does, but instead of adding +1 every time a new child is added, it adds all the existing children every time I open the activity.
Say, support in table, the value is 10 and I have 5 children in the RecyclerView
, and I open it, and before I even receive a new child, the table gets +5 added because there are 5 existing children.
I don't want this to happen, but I want +1 to be added only when a new child is added. How can I achieve this?
@Override
protected void onBindViewHolder(@NonNull final Chat.MessagesViewHolder holder, final int position, @NonNull final MessagesHelper model) {
holder.setMessage(model.getMessage());
final String userId = getRef(position).getKey();
final DatabaseReference mTimeReference = FirebaseDatabase.getInstance().getReference().child("Messages").child(MessageSenderId).child(MessageRecieverId);
Query messageQuery = mTimeReference.limitToLast(10);
messageQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
MessagesHelper message = dataSnapshot.getValue(MessagesHelper.class);
mMessages();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});