Hide the selected item from the custom spinner list

元气小坏坏 提交于 2019-12-11 10:49:32

问题


I have a Spinner displaying some items via an Adapter. The thing is everytime I click on the spinnerm it shows the list of all items that are selectable by the user. I would like to hide the item currently selected from the list.

Example:

Here is my list of Items :

Selected: Item A

Spinner List:

  • Item A
  • Item B
  • Item C

If I select Item B, it will become:

Selected: Item B

Spinner List:

  • Item A
  • Item B
  • Item C

I would like to hide the selected item from the Spinner List. So, in the two previous cases:

Selected: Item A

Spinner List:

  • Item B
  • Item C

If I select Item B, it will become:

Selected: Item B

Spinner List:

  • Item A
  • Item C

回答1:


You should create a list of seleted items. So everytime you select an item, you put on this list. After that you compare the two lists: the one with all values, with the one with the selected values and display only the items that aren't already selected. I've already used something like this:

    ArrayList<String> allItems = new ArrayList<String>();
    ArrayList<String> selectedItems = new ArrayList<String>();
    allItems.add("item a"); 
    allItems.add("item b"); 
    allItems.add("item c");

    selectedItems.add("item a");

    ArrayList<String> auxList = new ArrayList<String>();

    //populate an aux list without the selected items
    for(String itemFromAll: allItems){
        for(String selectedItem: selectedItems){
            if(!itemFromAll.equals(selectedItem)){
                auxList.add(itemFromAll);
            }
        }
    }

    //print the new list without the selected items
    for(String newItem: auxList){
        System.out.println(newItem);
    }

I hope it helps



来源:https://stackoverflow.com/questions/26307553/hide-the-selected-item-from-the-custom-spinner-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!