Hide the selected item from the custom spinner lis

2019-08-05 09:09发布

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条回答
萌系小妹纸
2楼-- · 2019-08-05 09:55

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

查看更多
登录 后发表回答