gpt4 book ai didi

java - 从 RecyclerView 中移除对象

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:29 26 4
gpt4 key购买 nike

使用 ListView 时,我可以通过编写 this.adapter.delete(mYeets.get(position)) 轻松地从适配器中删除一个项目。使用 RecyclerView,是否有等效项?还是通知适配器我现在必须做的所有更改?

private void deleteComment(int position) {
String currentUserObjectId = ParseUser.getCurrentUser().getObjectId();
ParseQuery<ParseObject> query = new ParseQuery<>("Yeet");
query.whereEqualTo(ParseConstants.KEY_OBJECT_ID, mYeets.get(position).getObjectId());
query.whereContains(ParseConstants.KEY_SENDER_ID, currentUserObjectId);
query.findInBackground((yeet, e) -> {
if (e == null) {

for (ParseObject yeetObject : yeet) {

if (yeetObject.getParseObject(ParseConstants.KEY_SENDER_AUTHOR_POINTER).getObjectId().equals((ParseUser.getCurrentUser().getObjectId()))) {

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
dialogBuilder.setTitle("Delete");
dialogBuilder.setMessage("Do you want to delete this Yeet?");
dialogBuilder.setPositiveButton("Yes", (dialog, which) -> {

// Iterate over all messages and delete them
for (ParseObject delete : yeet) {

delete.deleteInBackground();

//this.adapter.delete(mYeets.get(position));
this.adapter.notifyDataSetChanged();

Toast.makeText(mContext, "Yeet deleted", Toast.LENGTH_SHORT).show();
}

});
dialogBuilder.setNegativeButton("No", (dialog, which) -> {
});
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
}

} else {
Log.e("Error", e.getMessage());
}
});
}

最佳答案

只需从 RecyclerView.Adapter 使用的数据结构 (List) 中删除项目,然后调用其 notifyItemRemoved()应该是最好的方法。

之前建议的notifyDataSetChanged()不必要地使列表中的每个项目都无效,而不仅仅是那些位于已删除项目之后的项目。

关于java - 从 RecyclerView 中移除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39574647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com