After useful comments in the chat, the removal function was added:

public class ImageAdapter extends BaseAdapter { private Context context; List<ImageUpload> imageUploads; int resource; public ImageAdapter(Context context, List<ImageUpload> imageUploads, int resource) { this.context = context; this.imageUploads = imageUploads; this.resource = resource; } public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View gridView = (View) convertView; if (convertView == null) { gridView = new View(context); if (imageUploads.get(position).getOutputMetadata().getFilename().equals("null")) { gridView = inflater.inflate(R.layout.item_upload_default, null); ImageView imageView = (ImageView) gridView.findViewById(R.id.iv_upload); imageView.setImageResource(resource); } else { gridView = inflater.inflate(R.layout.item_upload, null); ImageView imageView = (ImageView) gridView.findViewById(R.id.iv_upload); ImageView closeView = (ImageView) gridView.findViewById(R.id.photo_close_btn); final ProgressWheel progress = (ProgressWheel) gridView.findViewById(R.id.loader_photo); ImageUpload mobile = imageUploads.get(position); if (imageUploads.get(position).isLoadImage()) Picasso.with(context) .load(StringUtil.URLADS_THUMBLER_RESIZE + 150 + "x" + 150 + "/" + StringUtil.URLADS_THUMBLER_IMAGE + mobile.getOutputMetadata().getFilename()) .into(imageView, new Callback() { @Override public void onSuccess() { progress.setVisibility(View.GONE); } @Override public void onError() { } }); else { File f = new File(imageUploads.get(position).getPath()); Picasso.with(context) .load(f) .into(imageView); } closeView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageUploads.remove(position); } }); } } return gridView; } 

But there was a new problem, after I click on the delete button, I get an error:

 java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 

And points to this line ... imageUploads.remove (position);

  • 2
    narrate onClickListener on photo_close_btn, and delete the image from imageUploads by clicking, then do notifyDataSetChanged - Vladyslav Matviienko
  • I’m writing something like this doesn't work ((imageView.setOnClickListener (new View.OnClickListener () {@Override public void onClick (View v) {list.remove (position);}}); - Inkognito
  • @metalurgus will I need to change my adapter by adding a ViewHolder to it? - Inkognito
  • What exactly do you write, what exactly does your not work ? What does the ViewHolder mean? - Vladyslav Matviienko
  • @metalurgus wrote as you advised me to assign id onClickListener and then delete the element from imageUploads. I wrote the code above as I tried to do it. Please tell me what is wrong? I ask about ViewHolder, because I additionally looked at examples. - Inkognito

1 answer 1

Install photo_close_btn from your photo_close_btn , in which you should remove the necessary element from imageUploads : imageUploads.remove(position); , and do not forget to call notyfyDataSetChanged(); after this notyfyDataSetChanged();

  • is it possible to make it delete the selected item, and not in descending order?) - Inkognito