So the scenario of my problem is I want to delete one row from the RecyclerView
but it give me the error
Inconsistency detected. Invalid view holder adapter
My code snippet are:
@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
}
And this is the code of my dialog:
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);
}
}
So the problem is when I called removeItemFromView
from my Interface Implemented method It does not update onBindViewHolder
want to know how to notify onBindViewHolder
that an item is removed.
try this code in your 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();
}
}
First the getItemCount should return the size of the list. Here , imageGalleryList.
Then just call notifyItemRemoved(position) alone.
public void removeItemFromView(int position) {
this.imageGalleryList.remove(position);
notifyItemRemoved(position);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.