我正在尝试使用ItemTouchHelper onSwiped方法实现 swip-to-delete 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);
}
}
附上我的Adapter类供参考。我认为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);
}
}
}
答案 0 :(得分:2)
一个解决方案是
在adpter中写getID
方法return
id 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
}