javaandroidnotifydatasetchangedonlongclicklistener

How do can I use this reload() method to the onLongClick method?


I am applying a delete function to my notes app, but when I delete something, it doesnt changes on the screen until I do some action that reloads it, like creating a new note, or accesing a note and the exiting it (even the one deleted). Only then, the deleted note will disappear, even though I clearly used the reload function. Here is my adapter, where I have the problem.

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

Please help me, I ve working on this for days now, and I am stuck at a simple problem


Solution

  • Reload data after the database has been updated. Invoke reload() after deleting a note:

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