简体   繁体   中英

How can I remove an item from a spinner when it has been already selected on another spinner

have a 3 spinners in my code with the same 4 choices however I would like that when a choice is selected on a certain spinner, that same choice gets removed from the next one so that no spinners can have the same choices. Can someone help me please?

You could create a List of choices, when the user selects an Item it is added to the List and that Item is removed from the other List that backs all 3 spinners. Then you notify the changes to the list.

List<String> choices = new ArrayList<>();
List<String> options = new ArrayList<>():

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
          choices.add(options.get(position));
          options.remove(position);
          adapter1.notifyDataSetChanged();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {}

});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
          choices.add(options.get(position));
          options.remove(position);
          adapter2.notifyDataSetChanged();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {}

});
spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
          choices.add(options.get(position));
          options.remove(position);
          adapter3.notifyDataSetChanged();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {}

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM