I have a list of items. I click on the first element and its sequence number becomes 1. Then I click on the second element - its sequence number is 2. On the third one - 3 and so on. Then I again click on the fifth and first elements and their sequence numbers are deleted. I can’t make it so that when you click on the fifth, and then on the first element, their sequence numbers become 1 and 5, respectively. And so by analogy with an arbitrary number of elements and an arbitrary choice of sequence numbers.

By task context: I have a set of cards with values ​​and I need to select a sequence in which to display these cards on the next screen.

  • The size of the list is known in advance and does not change? - iksuy
  • @iksuy yes, known and does not change. - Alexander Lomovskiy
  • one
    And what's the problem to add positions in the ArrayList ? If the number is already there, then delete, if not - add. - Flippy
  • @Flippy if I select 50 elements, and then remove 2, then I have 2 numbers released, then I need to take the minimum of these 2, too many conditions, I thought there is some more elegant way to do it ... - Alexander Lomovskiy
  • Collections automatically change their size and there will be no “holes” - Flippy

2 answers 2

To store the id and position, you can create a wrapper class for the selected element:

 pulic class SelectedItem{ private int id; private int position; public SelectedItem(int id, int position){ this.position = position; this.id = id; } public int getId(){ return id; } public int getPosition(){ return position; } } 

When you click to check the collection of selected items for the presence of id and delete it when it is found:

 List<SelectedItem> selectedItems = new ArrayList<>(); private boolean haveInclusion(int id){ List<SelectedItem> itemsForDelete = new ArrayList<>(); if(!selectedItems.isEmpty()){ for(SelectedItem item : selectedItems){ if(item.getId() == id){ itemsForDelete.add(item); } } if(!itemsForDelete.isEmpty()){ selectedItems.removeAll(itemsForDelete); } } return !itemsForDelete.isEmpty(); } 

And add an item as needed:

 public void onClick(View v){ int id = v.getId();//если необходимо проверять не id view-компонента передавайте его здесь if(!haveInclusion(id){ selectedItems.add(new SelectedItem(id, position/*здесь передавайте позицию элемента*/); } } 

UPD:

If you use this scheme, it is necessary before sending to the display, just sort the collection by position

  • Not so, get ConcurrentModificationException - Yura Ivanov
  • @YuraIvanov thanks, corrected - zTrap
 List<Integer> selectionList = new ArrayList<>(); void onClick(View v){ Integer position = holder.getAdapterPosition();// для примера if(!selectionList.remove(position)) //удаляем selectionList.add(position); //и если нечего было удалять, добавляем в конец } 

UPD

 List<MyItem> selectionList = new ArrayList<>(); void onClick(View v){ int position = holder.getAdapterPosition();// для примера MyItem item = mItems.get(position); if(!selectionList.remove(item)) //удаляем selectionList.add(item); //и если нечего было удалять, добавляем в конец } 

UPD2 Option with holes

 List<MyItem> selectionList = new ArrayList<>(); void onClick(View v){ int position = holder.getAdapterPosition();// для примера MyItem item = mItems.get(position); int index = selectionList.indexOf(item); if(index<0) { // элемент еще не добавлен index = selectionList.indexOf(null); //ищем дырки if(index<0) { // дырок нет selectionList.add(item); } else { selectionList.set(index, item); } } else { // элемент добавлен ранее, отжимаем selectionList.set(index, null); // делаем дырку } } List<MyItem> getSelectionList() { // когда понадобится список, надо удалить дырки List<MyItem> result = new ArrayList<>(selectionList); //копия списка Iterator<MyItem> it = result.iterator(); while(it.hasNext()){ if(it.next()==null) //удаляем дырки it.remove(); } return result; // список без дырок } 
  • In this case, the remove() method will return an Integer object, not a boolean , since it will be interpreted as a method of removing by position. - zTrap
  • 2
    @zTrap thanks corrected. - Yura Ivanov
  • @YuraIvanov thanks for the answer, but, ArrayList does not suit me, because I need to save also the id element with its position in the queue - Alexander Lomovskiy
  • @AlexanderLomovskiy added the answer. The list itself is needed to store the sequence, and what specifically is stored in this list is not so important. This can be MyObject, Pair <Integer, Integer> ... Using the side effects of TreeHashSet or TreeHashMap, preserving the order of adding elements is certainly not easier. - Yura Ivanov
  • @YuraIvanov While writing the answer, you had time to add: D - zTrap