0

My project displaying lists according to the users's information

EventChangeListener() classify people based on mbti, region, etc ..

When I run this function

private void EventChangeListener() {

    firestore.collection("Users")
            //.whereEqualTo("mbti","ENTP")
            //.whereEqualTo("region","")
            .whereEqualTo("gender","")
            .orderBy("time",Query.Direction.DESCENDING).limit(50)
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                    if(error != null){

                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }

                        Log.e("Firestore error",error.getMessage());
                        return;

                    }

                    for (DocumentChange dc : value.getDocumentChanges()){

                        if (dc.getType() == DocumentChange.Type.ADDED){
                            userArrayList.add(dc.getDocument().toObject(User.class));
                        }

                        myAdapter.notifyDataSetChanged();
                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }
                    }

                }
            });
}

Lists appears.

And when I try to load other lists like this function

firestore.collection("Users")
            .whereEqualTo("mbti","ENTP")
            .whereEqualTo("region","")
            .whereEqualTo("gender","")
            .orderBy("time",Query.Direction.DESCENDING).limit(50)
            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                @Override
                public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                    if(error != null){

                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }

                        Log.e("Firestore error",error.getMessage());
                        return;

                    }

                    for (DocumentChange dc : value.getDocumentChanges()){

                        if (dc.getType() == DocumentChange.Type.ADDED){
                            userArrayList.add(dc.getDocument().toObject(User.class));
                        }

                        myAdapter.notifyDataSetChanged();
                        if(progressDialog.isShowing()){
                            progressDialog.dismiss();
                        }
                    }

                }
            });

The list is duplicated below the list that has already appeared.

I want the recyclerview to be initialized and only a new list will appear.

How can I do this?

Eye Lamp
  • 69
  • 5
  • Have you created an [index](https://stackoverflow.com/questions/50305328/firestore-whereequalto-orderby-and-limit1-not-working)? – Alex Mamo Feb 26 '22 at 09:01
  • @AlexMamo Thank you! I solved the index by clicking URL, but still, when I call a function in recyclerview, user lists are created below the previously created list. I want the previously created list to be gone. But I don't know about solving code. – Eye Lamp Feb 27 '22 at 15:15
  • @AlexMamo userArrayList.clear(); //recyclerView.removeAllViewsInLayout(); Using these codes solved it!.. – Eye Lamp Feb 28 '22 at 16:40

1 Answers1

1

I'm not sure what is the problem. If the list is duplicated and you want the new one to be replaced for the old one just empty your current list (I guess you use userArrayList to show items in the recycler view) and add the new content.

What I mean is to re-initialize the list before adding new content if you don't want to duplicate content.

Juan P.
  • 76
  • 8
  • Yes, but I have a problem initializing the list... I don't know about solving code. – Eye Lamp Feb 27 '22 at 15:18
  • If you could explain me more about what happens when you try to initialize the list I might be able to help you – Juan P. Feb 28 '22 at 10:45
  • For example when I start function 'a' -> then list shows a/b/c when I start function 'b' -> then list shows a/b/c/d/e/f but I want the list showing d/e/f.. – Eye Lamp Feb 28 '22 at 16:16
  • So I'm searching about that, but it is hard to search. – Eye Lamp Feb 28 '22 at 16:17
  • 1
    userArrayList.clear(); recyclerView.removeAllViewsInLayout(); ->> I solved it! – Eye Lamp Feb 28 '22 at 16:39
  • 1
    Great! Im glad you make it, ".clear()" works too for clearing the list. But be careful with the use of "removeAllViewsInLayout", it could bug your list and stop showing the items as you are removing the view the are loaded. – Juan P. Feb 28 '22 at 16:44