There are tentatively photos that I add to the GridView.

At the moment, I delete them using the following method:

closeView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageUploads.remove(position); notifyDataSetChanged(); } }); 

But the problem is that it removes the last position, regardless of which element was pressed.

It is necessary to delete an element by the position that was clicked. The most interesting thing is that in the logs it clicks on the position correctly.

  • what exactly does not work? Do something by clicking on an item, or delete a specific item? - Vladyslav Matviienko
  • By clicking on the element so that it is deleted. That is, we added 5 elements in the table, and then when you click on one of them, the selected element is deleted. - Morozov
  • These are 2 different problems: 1) perform the action by pressing; 2) delete the item. Which one do you have? - Vladyslav Matviienko
  • Well, it turns out two problems. First, I think you can write the onDelete method in xml and then just assign the handler. But I don’t really imagine how to deal with the second. Found many examples with the listView, but not that ( - Morozov
  • GridView is 98% ListView. The difference is that the elements are displayed differently. Never write methods in xml in your life - if you change something in the code, or in xml, it can lead to a lot of non-obvious errors. Install all liners in java-code. - Vladyslav Matviienko September

1 answer 1

To execute code by clicking on an item, install the OnItemClickListener from the GridView :

 gridview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { //тут действия, которые надо выполнить по клику на элемент. } }); 

To remove an item from the GridView , you need to remove it from the adapter (or from the adapter source), and call the adapter method notifyDataSetChanged();

  • Inside something like that? liste.remove (position); adapter.notifyDataSetChanged (); Tipo so? - Morozov
  • @VadimMorozov, yes, something like this - Vladyslav Matviienko
  • the question was how to register the remove method, you wrote off the method of clicking on the element ... - Morozov
  • @VadimMorozov, depends on how you store the photos in the list - Vladyslav Matviienko
  • can you explain? - Morozov