通过 ItemTouchHelper RecyclerView 滑动删除?
Swip to delete though ItemTouchHelper RecyclerView?
我正在尝试使用 ItemTouchHelper onSwiped 方法实现滑动删除 RecyclerView
。问题是,它给了我们适配器位置。但是如何从游标加载器中检索项目的实际 sql id?我正在使用 CursorLoader
和 ContentProvider
。请指导
public class ActivityTaskList extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
......
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int pos1 = viewHolder.getAdapterPosition();
deleteData(pos1);
Log.d(TAG,"onSwiped");
};
private void deleteData(long adapterPosition) {
Uri uri = Uri.parse(TaskProviderP.CONTENT_URI + "/"+ (int)adapterPosition);
getContentResolver().delete(uri, null,null);
Log.d(TAG,"deleteData");
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Log.d(TAG, "onCreateLoader");
String[] projection = { TaskTableP.COLUMN_ID, TaskTableP.COLUMN_NAME,TaskTableP.COLUMN_DESCRIPTION };
CursorLoader cursorLoader = new CursorLoader(this,
TaskProviderP.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Log.d(TAG, "onLoadFinished");
taskAdapterP.setCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d(TAG, "onLoaderReset");
taskAdapterP.setCursor(null);
}
}
附上我的适配器class以供参考。我认为 Nilesh 的解决方案需要某种列表或数组。我是新蜜蜂,对此感到困惑。
public class TaskAdapterP extends RecyclerView.Adapter<TaskAdapterP.TViewHolder> {
private Context context;
public TaskAdapterP(Context context) {
this.context = context;
setHasStableIds(true);
}
private Cursor cursor;
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
public void setCursor(Cursor cursor) {
this.cursor = cursor;
Log.d(TAG, "setCursor");
notifyDataSetChanged();
}
@Override
public TViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.one_task_row, parent, false);
Log.d(TAG, "onCreateViewHolder");
return new TViewHolder(view);
}
@Override
public void onBindViewHolder(TViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder");
if (cursor != null) {
cursor.moveToPosition(position);
holder.vhTask_Title.setText(cursor.getString(cursor.getColumnIndexOrThrow(TaskTableP.COLUMN_NAME)).replaceAll("\<[^>]*>", ""));
holder.vhTask_Description.setText(cursor.getString(cursor.getColumnIndexOrThrow(TaskTableP.COLUMN_DESCRIPTION)));
}
}
@Override
public int getItemCount() {
Log.d(TAG, "getItemCount");
return cursor == null ? 0 : cursor.getCount();
}
class TViewHolder extends RecyclerView.ViewHolder {
TextView vhTask_Title, vhTask_Description;
public TViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "TViewHolder Constructor");
vhTask_Title = (TextView) itemView.findViewById(R.id.textview_task_name);
vhTask_Description = (TextView) itemView.findViewById(R.id.textview_task_description);
}
}
}
一个解决方案是
在适配器中编写 getID
方法,它将 return
所提供的 positioin
的 id
public int getID(int positioin) {
return visitingCards.get(positioin).getNo();
}
然后像这样使用适配器对象调用 getID
mAdapter.getID(viewHolder.getAdapterPosition())
更新
对于你的适配器,你可以像
public int getID(int positioin) {
return cursor.moveToPosition(position).cursor.getXXX(YOUR_COLUM_NAME);// so that will return id
}
我正在尝试使用 ItemTouchHelper onSwiped 方法实现滑动删除 RecyclerView
。问题是,它给了我们适配器位置。但是如何从游标加载器中检索项目的实际 sql id?我正在使用 CursorLoader
和 ContentProvider
。请指导
public class ActivityTaskList extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
......
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int pos1 = viewHolder.getAdapterPosition();
deleteData(pos1);
Log.d(TAG,"onSwiped");
};
private void deleteData(long adapterPosition) {
Uri uri = Uri.parse(TaskProviderP.CONTENT_URI + "/"+ (int)adapterPosition);
getContentResolver().delete(uri, null,null);
Log.d(TAG,"deleteData");
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Log.d(TAG, "onCreateLoader");
String[] projection = { TaskTableP.COLUMN_ID, TaskTableP.COLUMN_NAME,TaskTableP.COLUMN_DESCRIPTION };
CursorLoader cursorLoader = new CursorLoader(this,
TaskProviderP.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Log.d(TAG, "onLoadFinished");
taskAdapterP.setCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
Log.d(TAG, "onLoaderReset");
taskAdapterP.setCursor(null);
}
}
附上我的适配器class以供参考。我认为 Nilesh 的解决方案需要某种列表或数组。我是新蜜蜂,对此感到困惑。
public class TaskAdapterP extends RecyclerView.Adapter<TaskAdapterP.TViewHolder> {
private Context context;
public TaskAdapterP(Context context) {
this.context = context;
setHasStableIds(true);
}
private Cursor cursor;
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
public void setCursor(Cursor cursor) {
this.cursor = cursor;
Log.d(TAG, "setCursor");
notifyDataSetChanged();
}
@Override
public TViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.one_task_row, parent, false);
Log.d(TAG, "onCreateViewHolder");
return new TViewHolder(view);
}
@Override
public void onBindViewHolder(TViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder");
if (cursor != null) {
cursor.moveToPosition(position);
holder.vhTask_Title.setText(cursor.getString(cursor.getColumnIndexOrThrow(TaskTableP.COLUMN_NAME)).replaceAll("\<[^>]*>", ""));
holder.vhTask_Description.setText(cursor.getString(cursor.getColumnIndexOrThrow(TaskTableP.COLUMN_DESCRIPTION)));
}
}
@Override
public int getItemCount() {
Log.d(TAG, "getItemCount");
return cursor == null ? 0 : cursor.getCount();
}
class TViewHolder extends RecyclerView.ViewHolder {
TextView vhTask_Title, vhTask_Description;
public TViewHolder(View itemView) {
super(itemView);
Log.d(TAG, "TViewHolder Constructor");
vhTask_Title = (TextView) itemView.findViewById(R.id.textview_task_name);
vhTask_Description = (TextView) itemView.findViewById(R.id.textview_task_description);
}
}
}
一个解决方案是
在适配器中编写 getID
方法,它将 return
所提供的 positioin
public int getID(int positioin) {
return visitingCards.get(positioin).getNo();
}
然后像这样使用适配器对象调用 getID
mAdapter.getID(viewHolder.getAdapterPosition())
更新 对于你的适配器,你可以像
public int getID(int positioin) {
return cursor.moveToPosition(position).cursor.getXXX(YOUR_COLUM_NAME);// so that will return id
}