Eye Lamp
Eye Lamp

Reputation: 69

Android firebase firestore how to initialize recyclerview

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?

Upvotes: 0

Views: 220

Answers (1)

Juan P.
Juan P.

Reputation: 166

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.

Upvotes: 1

Related Questions