Home > Blockchain >  Keeping updated my data at Firebasefirestore is causing duplicating my data in Android Studio Reycyc
Keeping updated my data at Firebasefirestore is causing duplicating my data in Android Studio Reycyc

Time:11-19

I created a recyclerview that retrieves items from firebasefirestore when I delete a data from firebasefirestore recyclerview refresh itself normally but when I add an item to firebasefirestore it is refreshed but dublicates the datas and so many items appears in the recyclerview but it shouldnt, and when I enter other activity then exit to this activity a few times my recyclerview backs to normal.This is my method to fill the recyclerview:

private void fillArray() {
    NoteArray = new ArrayList<>();

    //SnapshotListener read and write data in real time 
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
       
        @Override
        public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
            if(error !=null){
                Toast.makeText(recyclerview_notes.this,"Error " error.toString(),Toast.LENGTH_SHORT).show();
            }


            if (value!=null && !value.isEmpty()){
               
                  for(QueryDocumentSnapshot docs:value) {
                      Note note = docs.toObject(Note.class);
                              NoteArray.add(note);
                            
                }
                recyclerViewAdapter = new NotesRecyclerViewAdapter(NoteArray,recyclerViewInterface);
                recyclerView.setAdapter(recyclerViewAdapter);
                recyclerViewAdapter.notifyDataSetChanged();
          
            }

        }
    });

1.first I have 2 items in recyclerview:

2.then I added third item

3.it saves item but dublicates all items

4.when I enter other activity and exit a few times it is fixed

I want that when I add a data to firebase , my recyclerview updates normally.

CodePudding user response:

I think it shows the same data so many times but stored properly on database. Then just clear your list before adding data to the list. Like this NoteArray.clear();

See the updated code below

private void fillArray() {
    NoteArray = new ArrayList<>();

    //SnapshotListener read and write data in real time 
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
       
        @Override
        public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
            if(error !=null){
                Toast.makeText(recyclerview_notes.this,"Error " error.toString(),Toast.LENGTH_SHORT).show();
            }


            if (value!=null && !value.isEmpty()){
                  NoteArray.clear(); // adding list clear function. If data is fetched successfully then the list gets cleared old data and fills new data.
                  for(QueryDocumentSnapshot docs:value) {
                      Note note = docs.toObject(Note.class);
                              NoteArray.add(note);
                            
                }
                recyclerViewAdapter = new NotesRecyclerViewAdapter(NoteArray,recyclerViewInterface);
                recyclerView.setAdapter(recyclerViewAdapter);
                recyclerViewAdapter.notifyDataSetChanged();
          
            }

        }
    });

Hope it helps

  • Related