代码询问用户是否要从recyclerview / mysql数据库中删除条目。该功能有效,但是,如果用户未选择任何项目,则仍需要从回收者视图中删除该项(临时),直到刷新屏幕,然后它才会回来。我尝试将removeItem(position)方法移动到yes / no方法中。但是,它声明必须将变量设为最终变量。如果我最终确定,它将破坏功能。我该如何以这种方式排列代码,即当用户单击时不会删除视口?
/**
* Method which removes a row from the recycler-view view-holder. Also captured the cbtId Primary Key which is sent
* via Retrofit to the server to ensure the correct row is deleted from the database.
* @param position
*/
public void removeItem(int position) {
cbtId = mWorkoutLogList.get(position).getCbtId();
mWorkoutLogList.remove(position);
mWorkoutLogAdapter.notifyItemRemoved(position);
}
/**
* On click method which calls the deleteLog() method to delete the row from the recycler view.
* @param position
*/
@Override
public void onDeleteClick(int position) {
deleteLog(position);
}
/**
* Method which uses Retrofit to send a call to the MYSQL server to delete a workout log.
*/
private void deleteLog(int position){
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Are you Sure?");
builder.setMessage("Deleted Workout Logs cannot be restored.");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Call<WorkoutLogList> call = RetrofitClient.getInstance().getApi().deleteLog(cbtId);
call.enqueue(new Callback<WorkoutLogList>() {
@Override
public void onResponse(Call<WorkoutLogList> call, Response<WorkoutLogList> response) {
removeItem(position);
}
@Override
public void onFailure(Call<WorkoutLogList> call, Throwable t) {
}
});
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog ad = builder.create();
ad.show();
}
答案 0 :(得分:0)
将位置变量最终确定并移动到正确的位置即可解决此问题。
@Override
public void onClick(DialogInterface dialog, int which) {
removeItem(position);
Call<WorkoutLogList> call = RetrofitClient.getInstance().getApi().deleteLog(cbtId);
call.enqueue(new Callback<WorkoutLogList>() {
@Override
public void onResponse(Call<WorkoutLogList> call, Response<WorkoutLogList> response) {
}
removeItem(posiiton)方法仅在改装数据库调用之前放置,因为此方法获取cbtId,如果没有该id,则无法从数据库中删除行。