我的RecyclerView的Adapter文件中有一个LongClickListener,用于在RecyclerView列表中侦听CardViews上的LongClicks。监听器链接到RecyclerView活动文件的接口。
在LongClick启动DialogFragment后,步骤1为用户确认删除CardView。该对话框显示“取消”或“确定”选项。 “取消”只是否定了DialogFragment。
步骤2是选择“确定”并且DatabaseHelper文件中的删除代码将从SQLite数据库中删除CardView数据。
步骤3将再次运行RecyclerView Activity,以便RecyclerView列表刷新并显示更新的列表(自数据在数据库中删除后删除了之前的CardView)。
我对第1步很好,因为任何CardView上的LongClick都会按预期启动DialogFragment。我被困在片段行“dbHelper.deletefromDB(UserData userdata);”中的第2步。 Android Studio警告消息是“无法解析符号'userdata'”。我在这里错过了什么?下面显示了相关的各种文件。
ListActivity file:
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dbList = dbHelper.getAllDataFromDB();
...
ListAdapter mAdapter;
mAdapter = new ListAdapter(this, dbList);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setonItemClickListener(new ListAdapter.ClickListener() {
@Override
public void onItemLongClick(int position, View view) {
FragmentManager fm = getSupportFragmentManager();
DeleteCardViewFragment delCardViewDialog = new DeleteCardViewFragment();
delCardViewDialog.show(fm, "delcardview dialog");
}
});
DatabaseHelper file:
public class DatabaseHelper extends SQLiteOpenHelper {
...
public void deletefromDB(UserData userData) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
db.delete(DBContract.DBEntry.TABLE_NAME_USERINPUTS, DBContract.DBEntry.COLUMN_NAME_ID + " = ?",
new String[]{String.valueOf(userData.getId())});
db.setTransactionSuccessful();
db.endTransaction();
if(db.isOpen())
db.close();
}
}
DeleteCardViewFragment file:
public class DeleteCardViewFragment extends DialogFragment {
DatabaseHelper dbHelper;
public DeleteCardViewFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.delcardview_layout, container, false);
getDialog().setTitle("Delete card");
dbHelper = new DatabaseHelper(getActivity());
Button btnCancel = (Button) rootView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getDialog().cancel();
}
});
Button btnOK = (Button) rootView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.deletefromDB(UserData userdata);
dismiss();
}
});
...
}
}