首页 > 解决方案 > notifyItemRemoved(); 在我退出并重新进入活动之前不会从列表中删除评论

问题描述

我正在尝试更新评论列表,我正在使用notifyItemInsertedand来更新notifyItemRemoved。当用户添加评论时,评论完美地添加到列表中,但是当我想删除评论时出现问题。我遇到的问题是,当我删除列表中的评论时,它不仅会立即删除评论,还会重新排列列表,并且只有在我退出活动然后重新进入时才会删除评论。

有人可以告诉我如何调整我的代码以便立即删除评论吗?

评论活动

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);
    }
}

标签: javaandroidandroid-recyclerview

解决方案


我的理解是,该onChildRemoved()方法只是为您提供已删除项目的键,而不是值。所以我们所做的就是将每个项目的键与项目本身一起存储(这样现在它们在同一个索引处)。当您删除一个项目onChildRemoved()时,它会触发并为您提供已删除项目的键......所以现在只需查找已删除键的索引,我们就可以安全地从commentsList.

有一个存储项目键的列表

public class CommentsActivity extends AppCompatActivity {

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

在添加注释的同时添加密钥:

@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);
}

现在,当您删除时:

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

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

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

mCommentAdapter.notifyDataSetChanged();

}

推荐阅读