0

Hi I am trying to update the firebase Realtime database.

I am able to successfully do this operation but the problem I am having is when I finish this operation I call notifyDataSetChanged() to update the recycler view although it doesn't work.

What ends up happening is that it creates duplicate rows. I have to manually refresh the activity to see the UI change take effect.
Usually when I call notifyDataSetChanged() the UI updates but it isn't for some reason.

Can anyone please help?

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Student").child(students.get(adapterPosition).getStudentID()).child(students.get(adapterPosition).getDatabaseSnapshotValue());

databaseReference.child("StudentAge").setValue(Integer.parseInt(age));
databaseReference.child("StudentName").setValue(newName);
new Handler(Looper.getMainLooper()).post(() -> {
    notifyDataSetChanged();
});

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I believe I have figured out an issue, I have noticed the duplication only occurs when I try and update more than one record in firebase but to combat that I thought if I call notifyDataSetChanged() after each update operation it would fix it but it didnt – John Smith Feb 09 '21 at 20:31
  • 1
    There is no need to call `notifyDataSetChanged()` after **writing** to Firebase. You should only call `notifyDataSetChanged()` after you **read** data, like in an `onDataChange` or `onChild...` method of a listener. – Frank van Puffelen Feb 09 '21 at 21:21
  • how can I update the UI then after update – John Smith Feb 09 '21 at 22:35
  • 1
    I assume you also *read* the data somewhere. If you use a `addValueEventListener` or `addChildEventListener` that will automatically be invoked with the new value from this right (straight away, even when you have no network connection), and you'd then call `notifyDataSetChanged` from within the `onDataChange` or `onChild...` method of the listener. – Frank van Puffelen Feb 09 '21 at 22:54

0 Answers0