0

I'm trying to retrieve comments from the database but it shows all the comments as duplicate

This same listner works fine with fragments but gives duplicate item bug when implemented on Activity

private void startListening() {
    Query query = mCommentsDatabase;

    FirebaseRecyclerOptions<Comment> options =
            new FirebaseRecyclerOptions.Builder<Comment>()
                    .setQuery(query, Comment.class)
                    .build();

    FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Comment, CommentsViewHolder>(options){
        @Override
        public CommentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.message for each item
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.single_comment_item, parent, false);
            return new CommentsViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(final CommentsViewHolder holder, int position, final Comment model) {

            mCommentsDatabase.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                    if (dataSnapshot.exists() && dataSnapshot != null){
                            if (dataSnapshot.exists()){
                                String CommenterId = dataSnapshot.child("uid").getValue().toString();
                                mUsersDatabase.child(CommenterId).addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot userDataSnapshot) {
                                        if (userDataSnapshot.exists()){
                                            String username = userDataSnapshot.child("username").getValue().toString();
                                            String thumbImage = userDataSnapshot.child("profile_thumbnail").getValue().toString();
                                            String comment = dataSnapshot.child("body").getValue().toString();
                                            Long timestamp = Long.parseLong(dataSnapshot.child("timestamp").getValue().toString());
                                            GetTimeAgo getTimeAgo = new GetTimeAgo();
                                            String time  = getTimeAgo.getTimeAgo(timestamp, getApplicationContext());

                                            holder.setName(username);
                                            holder.setComment(comment);
                                            holder.setImage(thumbImage, getApplicationContext());
                                            holder.setTime(time);
                                        }
                                    }

                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {

                                    }
                                });
                            }
                    }

                }

                @Override
                public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }

    };

    mCommentsList.setAdapter(adapter);
    adapter.startListening();
}

Can someone help point to what's wrong?

I've implemented the same thing in fragments it's working but when used in an Activity it works weirdly.

Dhiraj kadam
  • 377
  • 3
  • 15
  • why you are call adapter.startListening(); inside same function? – Mohammad Sommakia Jul 02 '18 at 14:08
  • I've set it under startListening method's scope. – Dhiraj kadam Jul 02 '18 at 14:16
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Jul 03 '18 at 13:10

0 Answers0