java - notifyItemRemoved(); 在我退出并重新进入活动之前不会从列表中删除评论
问题描述
我正在尝试更新评论列表,我正在使用notifyItemInserted
and来更新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);
}
}
解决方案
我的理解是,该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();
}
推荐阅读
- node.js - 如何在“from”和“to”区间内从子文档请求mongodb(mongoose)数据?
- java - 应用程序部署时应用程序引擎“非零退出:1”错误
- c - Eclipse 新手,需要帮助更改文本
- python - Plotly / Plotly-Dash:如何在图表中为每个图例嵌入数字+增量指标?
- webpack - 如何使用 webpacker 在 Svelte 中包含 Bootstrap 样式
- python - Telegram 通过频道 ID 获取消息
- python - 如何在屏幕上移动包含文本的 Rect?
- c# - 如何在后面的代码中绑定嵌入的图像
- sql - 在 MS Access 中使用 AND & OR 条件自定义查询条件
- swift - 在 Swift 中使用枚举的超类型作为协议或泛型