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(); } 
  • what about the code? is he invited to write to the respondent? In general, the adapter can get the record id in the current position, since the cursor is located in it. what to do next with this information is up to you, as there are no even hints at the implementation of the adapter. And in general, nothing is clear about what the consequences are and what the actual problem is - pavlofff
  • Updated the question, please look, in this thread you fumble, help again with the list :)) - Flippy

2 answers 2

ID records in the database and positions in the list are not connected in any way and working with the database on positions will not lead to anything good in the end. You need to get exactly the ID of the record being deleted from the database and work with it (only the main parts of your adapter and where the changes were):

 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); } } NotesAdapter(Cursor cursor, Context context) { this.cursor = cursor; this.context = context; this.activity = (NotepadActivity)context; } @Override public void onBindViewHolder(final AdapterHolder holder, int position) { cursor.moveToPosition(position); Long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); 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)); holder.delete.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { initDeleteDialog(id); } }); } } 

I would generally transfer the received ID through the callback to the activation and there I already displayed the delete dialog, made changes to the database and updated the adapter.

  • @ SergeyGrushin I also noticed cursor.moveToPosition(position); in your code cursor.moveToPosition(position); - this is superfluous, the adapter itself iterates the cursor (must iterate, as far as I remember) - pavlofff

Turned on the brains and did so

1) Created a method that creates / recreates a sheet of id-schnick 2) As id to delete the record, pass the id from this sheet to position 3) After deleting, re-create this sheet

  • one
    It is too difficult, resource-intensive and not efficient. There is no need to store all the IDs, and then also keep them up to date - the ID of the record to be deleted can be obtained directly from the cursor when you click the delete button. - pavlofff