Было приложение с ListView
, переделываю его под RecyclerView
c CardView
. Раньше строки из списка удалялись через контекстное меню. Пробовал прикрутить контекстное меню к RecyclerView
(для удаления) - не получилось...
Сейчас пробую удалить через LongClick
. Но если и получается удалить элемент то список не обновляется. Для чтения использую CursorLoader во фрагменте.
Подскажите как правильно организовать удаление ?
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();
}
}