繁体   English   中英

如果不将其设为Final,则无法访问内部类中的变量

[英]Cannot access variable inside inner class without making it Final

代码询问用户是否要从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();

}

使位置变量最终并将其移动到正确的位置可以解决问题。

 @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,无法从数据库中删除该行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM