gpt4 book ai didi

java - 删除笔记数组后如何重新加载笔记数组?

转载 作者:行者123 更新时间:2023-12-01 16:15:04 26 4
gpt4 key购买 nike

所以我正在做一个笔记应用程序。这真的很简单,我正在尝试添加一个功能,以便当我长按注释时,我可以将其删除。它可以工作,但是 notes ArrayList 删除后不会刷新。重要的是要说这些功能是在适配器中应用的。我有一个名为 refresh 的函数,顾名思义,它会在创建新笔记之类的内容时刷新 Activity 。因此,删除笔记后,如果我创建一个新笔记,则会调用刷新函数,并且之前删除的笔记会消失,但前提是调用重新加载函数。但是,由于某种原因,我无法使用 onLongClickListener 中的重新加载函数有人可以帮帮我吗。这是我的代码的副本:

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> {
public static class NoteViewHolder extends RecyclerView.ViewHolder {
public LinearLayout containerView;
public TextView nameTextView;

public NoteViewHolder(View view) {
super(view);
this.containerView = view.findViewById(R.id.note_row);
this.nameTextView = view.findViewById(R.id.note_row_name);

this.containerView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(final View view) {
final Note note = (Note) containerView.getTag();

PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
if (menuItem.getItemId() == R.id.delete) {
Toast.makeText(view.getContext(), "Note deleted", Toast.LENGTH_SHORT).show();
MainActivity.database.noteDao().delete(note.id);
return true;
} else {
return false;
}
}
});
popupMenu.inflate(R.menu.delete_menu);
popupMenu.show();

return true;
}
});

this.containerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Note note = (Note) containerView.getTag();
Intent intent = new Intent(v.getContext(), NoteActivity.class);
intent.putExtra("id", note.id);
intent.putExtra("content", note.content);

context.startActivity(intent);
}
});
}
}

private List<Note> notes = new ArrayList<>();

@Override
public NoteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_row, parent, false);

return new NoteViewHolder(view);
}

@Override
public void onBindViewHolder(NoteViewHolder holder, int position) {
Note current = notes.get(position);
holder.containerView.setTag(current);
holder.nameTextView.setText(current.content);
}

@Override
public int getItemCount() {
return notes.size();
}

public void reload() {
notes = MainActivity.database.noteDao().getAll();
notifyDataSetChanged();
}

}

最佳答案

不用担心,这很简单。

做一个小改变

recyclerViewAdapter.notifyDataSetChanged(position);

它会工作得很好。

关于java - 删除笔记数组后如何重新加载笔记数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62421643/

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