如何从回收站视图中删除项目时如何通知onBindViewHolder?

时间:2017-01-24 06:38:31

标签: android android-recyclerview android-viewholder

所以我的问题的情况是我想从RecyclerView中删除一行,但它会给我错误

  

检测到不一致。视图持有者适配器无效

我的代码段是:

     @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {

            holder.textViewTitle.setText(imageGalleryList.get(position).title);
            holder.textViewTitleDesc.setText(imageGalleryList.get(position).comment);
            holder.textViewImageCreateInfo.setText(imageGalleryList.get(position).createUserId + " "/*+ UtilityOfActivity.convertLongDateToShortDateTime(getJCImageBySerialResponse[position].createDate)*/);
            //  imageLoader.displayImage(getJCImageBySerialResponse[position].imageUrl, holder.uploadedImage);
            holder.btnDeleteImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {


                    imagePosition = position;
//calling the custom dialog

                    CustomDialogTwoButtons.showDialog(context, Constant.calledByDataSheetImageGallery,
                            Constant.calledBy_delete_image_gallery,
                            Constant.Confirm_Image_Deletion, Constant.Confirm_Image_Deletion_Message,
                            Constant.DialogPositiveButton, Constant.DialogNegativeButton);

                }
            });
// override interface method
     public void dialogItemClick(Context context, String calledBy) {

            Toast.makeText(context, " " + imagePosition, Toast.LENGTH_SHORT).show();
            removeItemFromView(imagePosition);



        }
//method to remove item from the recycler view row
 public void removeItemFromView(int position) {

        this.imageGalleryList.remove(position);

        notifyItemChanged(position);
        notifyItemRemoved(position);
        notifyDataSetChanged();


        //removes the row
    }

这是我对话框的代码:

public class CustomDialogTwoButtons extends Activity
{
    public static void showDialog(final Context context, final String calledByFragment,
                                  final String calledBy,
                                  String title, String message,
                                  String positiveButton, String negativeButton) {

        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(R.layout.custom_dialog_two_buttons);

        TextView txtTitle = (TextView) dialog.findViewById(R.id.txtTitle);
        TextView txtMessage = (TextView) dialog.findViewById(R.id.txtMessage);
        Button btnPositive = (Button) dialog.findViewById(R.id.btnPositive);
        Button btnNegative = (Button) dialog.findViewById(R.id.btnNegative);

        txtTitle.setText(title);
        txtMessage.setText(message);
        btnPositive.setText(positiveButton);
        btnNegative.setText(negativeButton);

        btnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(context, "Positive ", Toast.LENGTH_SHORT).show();

                CustomDialogInterface reference = null;

                if (calledByFragment.equalsIgnoreCase(Constant.calledByDataSheetOrderInfo)) {

                    if (calledBy.equalsIgnoreCase(Constant.DELETE_CUST_VOICE)) {
                        reference = new DataSheetOrderInfo();
                    }
                    else if (calledBy.equalsIgnoreCase(Constant.EDIT_CUST_VOICE)) {
                        reference = new DialogFragmentAddEditDeleteCustVoice();
                    } else if (calledBy.equalsIgnoreCase(Constant.ADD_CUST_VOICE)) {
                        reference = new DialogFragmentAddEditDeleteCustVoice();
                    }

                }
                else if(calledByFragment.equalsIgnoreCase(Constant.calledByCreateRepairOrderFragment))  {
                    if (calledBy.equalsIgnoreCase(Constant.DELETE_CUST_VOICE)) {
                        reference = new FragmentCreateRepairOrder();
                    } else if (calledBy.equalsIgnoreCase(Constant.EDIT_CUST_VOICE)) {
                        reference = new CustomDialogEditCustVoice();
                    } else if (calledBy.equalsIgnoreCase(Constant.ADD_CUST_VOICE)) {
                        reference = new FragmentCreateRepairOrder();
                    }
                }

                else if(calledByFragment.equalsIgnoreCase(Constant.calledByDataSheetImageGallery))  {

                    if (calledBy.equalsIgnoreCase(Constant.calledBy_delete_image_gallery)) {
                        reference = (CustomDialogInterface) new ImageUploadAdapter();
                    }
                }

                reference.dialogItemClick(context, calledBy);

                dialog.cancel();
            }

        });

        btnNegative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Negative ", Toast.LENGTH_SHORT).show();

                dialog.cancel();
            }
        });

        dialog.show();

    }

    public interface CustomDialogInterface {

         void dialogItemClick(Context context, String calledBy);
    }


}

所以问题是当我从我的Interface Implemented方法调用removeItemFromView时它没有更新onBindViewHolder想要知道如何通知onBindViewHolder项被删除。

2 个答案:

答案 0 :(得分:0)

removeItemFromView

中试用此代码
public void removeItemFromView(int position) {
    try{
      this.imageGalleryList.remove(position);
      notifyItemRemoved(position);
      notifyItemRangeChanged(position, imageGalleryList.size())
      //removes the row
    } catch (Exception e){
      notifyDataSetChanged();
      e.printStackTrace();
    }
}

答案 1 :(得分:0)

首先,getItemCount应该返回列表的大小。这里是imageGalleryList。

然后只需单独调用notifyItemRemoved(position)。

public void removeItemFromView(int position) {
    this.imageGalleryList.remove(position);
    notifyItemRemoved(position);
}