There was an application with ListView , I remake it under RecyclerView with CardView . Previously, lines from the list were deleted via the context menu. I tried to fasten the context menu to RecyclerView (for deletion) - it did not work ... Now I try to delete via LongClick . But if it turns out to delete the item, the list is not updated. For reading I use CursorLoader in a fragment.
Tell me how to organize the removal?
RecordAdapter
public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.ViewHolder> { Cursor dataCursor; Context context; public static class ViewHolder extends RecyclerView.ViewHolder { public TextView date; public TextView distance; public ImageView imageView; public ViewHolder(View v) { super(v); date = (TextView) v.findViewById(R.id.tvDate); distance = (TextView) v.findViewById(R.id.tvTitle); imageView = (ImageView) v.findViewById(R.id.ivImg); } } public RecordAdapter(Activity mContext, Cursor cursor) { dataCursor = cursor; context = mContext; } @Override public RecordAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View cardview = LayoutInflater.from(parent.getContext()) .inflate(R.layout.item, parent, false); return new ViewHolder(cardview); } public Cursor swapCursor(Cursor cursor) { if (dataCursor == cursor) { return null; } Cursor oldCursor = dataCursor; this.dataCursor = cursor; if (cursor != null) { this.notifyDataSetChanged(); } return oldCursor; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { dataCursor.moveToPosition(position); holder.itemView.setOnClickListener (new View.OnClickListener() { @Override public void onClick(View v) { if (dataCursor != null) { dataCursor.moveToPosition(position); Intent intent = new Intent(context, Note.class); long id = dataCursor.getLong(dataCursor.getColumnIndex(DB.COLUMN_ID)); intent.putExtra("id", id); context.startActivity(intent); } } }); holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext()); builder.setTitle ("Удаление") .setMessage ("Удалить ? " ) .setNegativeButton("Отмена",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // Тут хотел удалить элемент списка после чего нужно обновить список dataCursor.moveToPosition(position); long id = dataCursor.getLong(dataCursor.getColumnIndex(DB.COLUMN_ID)); DB notesDB = new DB(context); notesDB.open(); notesDB.delRec(DB.DB_TABLE_NOTES, id); notesDB.close(); notifyDataSetChanged(); } }); builder.create().show(); return true; } }); String date = dataCursor.getString(dataCursor.getColumnIndex(DB.COLUMN_DATE)); String title = dataCursor.getString(dataCursor.getColumnIndex(DB.COLUMN_TITLE)); String value = dataCursor.getString(dataCursor.getColumnIndex(DB.COLUMN_COLOR)); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyy"); Date resultdate = new Date(Long.parseLong(date)); String sdfDate = sdf.format(resultdate); if ( title.equals("")){ title = context.getResources().getString(R.string.non_title); } holder.date.setText(sdfDate); holder.distance.setText(title); GradientDrawable bgShape = (GradientDrawable) holder.imageView.getBackground(); bgShape.setColor( Integer.parseInt(value)); } @Override public long getItemId(int position) { return super.getItemId(position); } @Override public int getItemCount() { return (dataCursor == null) ? 0 : dataCursor.getCount(); } }