如何将此 reload() 方法用于 onLongClick 方法?
How do can I use this reload() method to the onLongClick method?
我正在对我的笔记应用程序应用删除功能,但是当我删除某些内容时,它不会在屏幕上发生变化,直到我执行一些重新加载它的操作,例如创建新笔记或访问笔记并退出它(甚至是删除的那个)。只有这样,删除的笔记才会消失,即使我明明使用了重新加载功能。这是我的适配器,我遇到了问题。
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> {
public static class NoteViewHolder extends RecyclerView.ViewHolder {
public LinearLayout containerView;
public TextView nameTextView;
public NotesAdapter adapter = new NotesAdapter();
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);
adapter.reload;
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();
}
}
请帮助我,我已经为此工作了好几天了,但我遇到了一个简单的问题
更新数据库后重新加载数据。删除笔记后调用reload()
:
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);
reload(); // reload data
return true;
} else {
return false;
}
}
});
我正在对我的笔记应用程序应用删除功能,但是当我删除某些内容时,它不会在屏幕上发生变化,直到我执行一些重新加载它的操作,例如创建新笔记或访问笔记并退出它(甚至是删除的那个)。只有这样,删除的笔记才会消失,即使我明明使用了重新加载功能。这是我的适配器,我遇到了问题。
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> {
public static class NoteViewHolder extends RecyclerView.ViewHolder {
public LinearLayout containerView;
public TextView nameTextView;
public NotesAdapter adapter = new NotesAdapter();
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);
adapter.reload;
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();
}
}
请帮助我,我已经为此工作了好几天了,但我遇到了一个简单的问题
更新数据库后重新加载数据。删除笔记后调用reload()
:
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);
reload(); // reload data
return true;
} else {
return false;
}
}
});