Colleagues, save, deadline is near. Work got up. There was no problem with this in ListView , since id passed to onItemClick . There is no such thing in RecyclerView , records are deleted and, accordingly, id from
1 2 3 4 Turn into
1 2 4 I delete the data row by position in the list. As a result, all logic is lost and after the first deletion the element is deleted before the selected one. Then two times before the selected, etc.
Adapter
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.AdapterHolder> { Cursor cursor; Context context; NotepadActivity activity; public class AdapterHolder extends RecyclerView.ViewHolder { TextView time, date, text; CardView card; ImageView edit, delete; AdapterHolder(View itemView) { super(itemView); time = (TextView)itemView.findViewById(R.id.note_item_time); date = (TextView)itemView.findViewById(R.id.note_item_date); text = (TextView)itemView.findViewById(R.id.note_text); card = (CardView)itemView; edit = (ImageView)itemView.findViewById(R.id.note_edit); delete = (ImageView)itemView.findViewById(R.id.note_delete); delete.setOnClickListener(delete_listener); } OnClickListener delete_listener = new OnClickListener(){ @Override public void onClick(View v) { initDeleteDialog(getAdapterPosition()+1); } }; } NotesAdapter(Cursor cursor, Context context) { this.cursor = cursor; this.context = context; this.activity = (NotepadActivity)context; } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } @Override public AdapterHolder onCreateViewHolder(ViewGroup viewGroup, int p2) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.note_item, viewGroup, false); AdapterHolder pvh = new AdapterHolder(v); return pvh; } @Override public void onBindViewHolder(final AdapterHolder holder, int position) { cursor.moveToPosition(position); String date = cursor.getString(cursor.getColumnIndex(Database.NOTES_DATE)); String time = cursor.getString(cursor.getColumnIndex(Database.NOTES_TIME)); String color = cursor.getString(cursor.getColumnIndex(Database.NOTES_COLOR)); String text = cursor.getString(cursor.getColumnIndex(Database.NOTES_TEXT)); holder.time.setText(time); holder.date.setText(date); holder.text.setText(text); holder.card.setCardBackgroundColor(Color.parseColor(color)); } @Override public int getItemCount() { return cursor.getCount(); } public void rebuild() { cursor.requery(); notifyDataSetChanged(); } void initDeleteDialog(final int position) { AlertDialog.Builder cansel_add_note = new AlertDialog.Builder(context); cansel_add_note.setTitle("Подтверждение"); cansel_add_note.setMessage("Удалить эту заметку?"); cansel_add_note.setPositiveButton("ДА", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); activity.delete(position); } }); cansel_add_note.setNegativeButton("НЕТ", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog cansel = cansel_add_note.create(); cansel.show(); } } delete method in activity
void delete(int position){ database.delete(Database.NOTES_TABLE, BaseColumns._ID +"=?", new String[]{Integer.toString(position)}); notes_adapter.rebuild(); }