0

I'm using this query for the recycler view:

private void load() {

    list.clear();

    Query query = firebaseFirestore.collection(collection_name).orderBy("time", Query.Direction.DESCENDING);
    query.addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

            if (queryDocumentSnapshots.size() == 0) {

                noDataText.setVisibility(View.VISIBLE);

            } else {

                noDataText.setVisibility(View.INVISIBLE);

                /*
                for (DocumentChange documentChange: queryDocumentSnapshots.getDocumentChanges()) {

                    if (documentChange.getType() == DocumentChange.Type.ADDED) {

                        String documentID = documentChange.getDocument().getId();
                        Data data = documentChange.getDocument().toObject(Data.class).withId(documentID);
                        list.add(data);

                        recyclerAdapter.notifyDataSetChanged();

                    } else if (documentChange.getType() == DocumentChange.Type.MODIFIED) {

                        String documentID = documentChange.getDocument().getId();
                        Data data = documentChange.getDocument().toObject(Data.class).withId(documentID);

                        if (documentChange.getOldIndex() == documentChange.getNewIndex()) {

                            // Item changed but remained in same position
                            list.set(documentChange.getOldIndex(), data);
                            recyclerAdapter.notifyItemChanged(documentChange.getOldIndex());

                        } else {

                            // Item changed and changed position
                            list.remove(documentChange.getOldIndex());
                            list.add(documentChange.getNewIndex(), data);
                            recyclerAdapter.notifyItemMoved(documentChange.getOldIndex(), documentChange.getNewIndex());

                        }

                        recyclerAdapter.notifyDataSetChanged();

                    } else if (documentChange.getType() == DocumentChange.Type.REMOVED) {

                        list.remove(documentChange.getOldIndex());
                        recyclerAdapter.notifyItemRemoved(documentChange.getOldIndex());

                    }

                }
                */

                for (DocumentChange documentChange : queryDocumentSnapshots.getDocumentChanges()) {

                    if (documentChange.getType() == DocumentChange.Type.ADDED) {

                        String documentID = documentChange.getDocument().getId();
                        Data data = documentChange.getDocument().toObject(Data.class).withId(documentID);
                        list.add(data);

                        recyclerAdapter.notifyDataSetChanged();

                    } else if (documentChange.getType() == DocumentChange.Type.MODIFIED) {

                        String documentID = documentChange.getDocument().getId();
                        Data data = documentChange.getDocument().toObject(Data.class).withId(documentID);
                        list.remove(data);
                        list.clear();
                        list.add(data);

                        recyclerAdapter.notifyDataSetChanged();

                    } else if (documentChange.getType() == DocumentChange.Type.REMOVED) {

                        String documentID = documentChange.getDocument().getId();
                        Data data = documentChange.getDocument().toObject(Data.class).withId(documentID);
                        list.remove(data);

                        recyclerAdapter.notifyDataSetChanged();

                    }

                }

            }

        }
    });

}

You will notice that there are 2 for() loops. The first is commented so for sure it will not affect the code. And the other which is being used right now.

The problem is that when there is only 1 document in firestore and when I delete that document, the textView is visible when this queryDocumentSnapshots.size() == 0 is reached. But the the document deleted is still shown in the view.

I tried the solution given here but nothing has changed.

What's the problem?

fsdklfm
  • 17
  • 6

0 Answers0