【问题标题】:Hide the selected item from the custom spinner list从自定义微调器列表中隐藏所选项目
【发布时间】:2014-10-10 20:26:14
【问题描述】:

我有一个 Spinner 通过适配器显示一些项目。问题是每次我单击 spinnerm 时,它都会显示用户可选择的所有项目的列表。我想隐藏当前从列表中选择的项目。

示例:

这是我的物品清单:

已选择:项目A

微调器列表:

  • 项目 A
  • B 项
  • C 项

如果我选择 B 项,它将变为:

已选择: B 项

微调器列表:

  • 项目 A
  • B 项
  • C 项

我想从微调器列表中隐藏所选项目。所以,在前两种情况下:

已选择:项目A

微调器列表:

  • B 项
  • C 项

如果我选择 B 项,它将变为:

已选择: B 项

微调器列表:

  • 项目 A
  • C 项

【问题讨论】:

  • 每个项目通常是布局中的一个视图,因此如果选择了当前视图,那么您只需将可见性设置为View.GONE,它将不再占用布局空间。您只需将更改通知您的适配器。适配器本身可能有更简单的方法。
  • 我尝试使用 View.GONE 但它也删除了微调器的选定项,如下所示:已选择:(空)微调器列表: - 项目 A - 项目 C
  • 哦,我误会了。我会让人跟进。我使用的微调器不够用。

标签: android spinner android-adapter


【解决方案1】:

您应该创建一个已选择项目的列表。所以每次你选择一个项目时,你都会放在这个列表中。之后,您比较两个列表:一个包含所有值,一个包含选定值,并仅显示尚未选择的项目。 我已经用过这样的东西:

    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);
    }

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2014-02-19
    • 2017-04-01
    相关资源
    最近更新 更多