简体   繁体   中英

notifyItemRemoved(); isn't removing the comment from the list until I exit and re-enter the activity

I am trying to update a comments list and I am doing so by using notifyItemInserted and notifyItemRemoved . When a user adds a comment, the comment is added to the list flawlessly, but the issue arises when I want to remove the comment. The issue that I am experiencing is that when I delete the comment in the list it doesn't just remove the comment right away, it rearranges the list, and the comment is only removed when I exit the activity and then re-enter.

Could someone tell me how to tweak my code so that the comment is removed right away?

CommentsActivity

public class CommentsActivity extends AppCompatActivity {

    private CommentAdapter mCommentAdapter;
    private List<Comment> mCommentList;
    private RecyclerView mRecyclerView;

    FirebaseUser mFirebaseUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);

        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mCommentList = new ArrayList<>();
        mCommentAdapter = new CommentAdapter(this, mCommentList, mPostId);
        mRecyclerView.setAdapter(mCommentAdapter);

        mAddComment = findViewById(R.id.add_comment);
        mImageProfile = findViewById(R.id.image_profile);
        mPost = findViewById(R.id.post_comment);

        getImage();
        readComments();
    }

    private void readComments() {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments").child(mPostId);
        ChildEventListener childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                Comment comment = dataSnapshot.getValue(Comment.class);
                mCommentList.add(comment);
                mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);

            }

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

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                Comment comment = dataSnapshot.getValue(Comment.class);

                int position = mCommentList.indexOf(comment);

                mCommentList.remove(comment);
                mCommentAdapter.notifyItemRemoved(position);
                mCommentAdapter.notifyItemRangeChanged(position, mCommentList.size());
            }

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

            }

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

            }
        };

        reference.addChildEventListener(childEventListener);
    }
}

What I understood is that the onChildRemoved() method just gives you the key of the removed item and not the value. So what we do is we store the key of each item along with item itself (so that now they are at the same index). When you remove an item the onChildRemoved() gets triggered and it will give you the key of the item that was removed.....So now just looking up the index of the removed key, we can safely remove the item itself from the commentsList .

Have a list that stores the keys of the item

public class CommentsActivity extends AppCompatActivity {

private CommentAdapter mCommentAdapter;
private List<Comment> mCommentList;
//add this
private List<String> keysList = new ArrayList<String>();
private RecyclerView mRecyclerView;
............

Add the key at the same time you add the comment:

@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
Comment comment = dataSnapshot.getValue(Comment.class);
mCommentList.add(comment);

//add this
keysList.add(dataSnapshot.getKey());

mCommentAdapter.notifyItemInserted(mCommentList.size() - 1);
}

Now when you remove:

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

int index = keysList.indexOf(dataSnapshot.getKey());

mCommentList.remove(index);
keysList.remove(index);

mCommentAdapter.notifyDataSetChanged();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM