1
Good evening! I’m making a chat app and I list the conversations in a recyclerview. I chose to use the addChildEventListener method, because (I think) it will consume less resources since the data will only be updated if there is a change in firebase. I can add conversations normally but found a problem when deleting. When I delete one of the conversations my recyclerview with the chat list does not update and it keeps appearing in the list even after it is deleted from firebase. The list is only updated if I close and open my app again. I know I should do something within the onChildRemoved method, but I don’t know how to make the list update. This is my method.
public void recuperarConversasPareamento(){
try {
listaConversas.clear();
childEventListenerChat = conversasParearExibicao.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Log.i("retornoChat", dataSnapshot.getValue().toString());
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Conversa conversa = ds.getValue(Conversa.class);
Log.i("retornoChat2", conversa.getUsuarioExibicao().getApelido());
Log.i("retornoChat2", conversa.getUsuarioExibicao().getEmail());
listaConversas.add(conversa); }
adapter.notifyItemInserted(listaConversas.size());
adapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
Conversa conversa = dataSnapshot.getValue(Conversa.class);
listaConversas.remove(conversa);
Log.i("log", "O dado foi removido");
Log.i("retornoChatRemovido", dataSnapshot.getValue().toString());
adapter.notifyDataSetChanged();
}
});
} catch (Exception e){
Log.i("erro-recuperarListaChat", e.getMessage());
}
And that’s the message structure I’m retrieving from firebase.