Q: hey there I'm creating a chat app and there is a problem in retrieving messages Hey there I'm creating a chat app and I store the messages in firebase database and similarly I retrieve from that. The problem for me is while retrieving the messages.
For that i use add value event listener . the thing i have done is:

*

*I place the location in on data change

*I add the message to the arraylist that i have connected to the recycler view

*after displaying the text to the recycler view i have deleted the message from that location itself

but before all of this i have added a if statement inside the ondata change and the condition is if(the datasnapshot value is not empty)then perform the above 3 steps. and amazingly it works.
but the only problem is whenever i got out of his/her chat activity and chat with someother and comes back to him i get his newly sent messages twice displaying and when i go again and come come back i get his newly sent messages thrice displaying so i dont know what to do thanks in advance...
my code

myfirebase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                mychildtext=dataSnapshot.getValue(String.class);
                if (!mychildtext.isEmpty())
                {
                    model d=new model();
                    //Here model is model class "List<model> modelList=new Arraylist()"
                    d.setTextMessage(mychildtext);
                    d.setIsme(false);
                    database.maindao().insert(d);
                    //I use database(RoomDB) and add all those stuff to it
                    modelList.clear();
                    modelList.addAll(database.maindao().getAll());
                    recyclerView.smoothScrollToPosition(modelList.size());
                    customAdapter.notifyDataSetChanged();
                    ref.child(temp).setValue("");
                    modelList.clear();
                    //above is the location of the message and so i clear it
                }
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

my source code
my output
my output

A: The reason why you are getting repetition is because the adapter has the previous content that was fetched on the first eventlistener. So when it fires again it will get all the previous data as well.
It is very messy handling the event listeners manually. What i can do is recommend you a fantastic library called FirebaseUI. It will handle for you the small details required to do the messages binding.
https://github.com/firebase/FirebaseUI-Android
It will handle updating the holder.
If you want to handle the event listeners you can use DiffUtil which can help you bind the data to your adapter and will handle your issue of repeating messages.
