【问题标题】:Adding item from Spinner dropdown从 Spinner 下拉列表中添加项目
【发布时间】:2016-05-29 03:08:39
【问题描述】:

我想要一个Spinner,其下拉列表的末尾包含一个元素,可以让我添加新项目。

任何想法如何实现这样的构造?

【问题讨论】:

  • 嗯,这是我的问题。我正在寻找如何制作这样的小部件的想法。因为到现在我心里什么都没有。 StackOverflow 不仅可以粘贴代码或修复错误。
  • 好吧,这类问题是题外话。见stackoverflow.com/help/on-topic
  • 得到严重帮助的类似问题示例 - stackoverflow.com/questions/26579230/… 但是谢谢!编辑:-一个特定的编程问题,或-一个软件开发独有的实用、可回答的问题因此我的问题不是题外话
  • @JakubW 创建一个微调器,旁边有一个图标(右侧)。单击图标时,显示一个对话框,允许用户输入文本(假设这是您想要的列表?)。当对话框关闭时,更新微调器的内容。对你们其他人;你不是很有帮助......

标签: android android-widget


【解决方案1】:

因为看起来“黄金”社区很自豪能够帮助我,所以我尝试了一些想法,看起来这个想法可能相当不错。

我创建了自定义ArrayAdapter。它的作用是在List 的末尾添加额外的虚假元素以作为NewItemBtn 工作。

代码

适配器

public class AddingSpinnerAdapter extends ArrayAdapter<String> {

public AddingSpinnerAdapter(Context context, int resource, List<String> objects) {
    super(context, resource, objects);
    addStaticElementToList(objects);
}

private void addStaticElementToList(List<String> objects) {
    objects.add("Add new objectt");
    notifyDataSetChanged();
}

}

微调器

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            int itemCount = parent.getAdapter().getCount() - 1;
            if (itemCount == position) {
                //bogus element chosen 
            } else {
                //select element
            }
        }

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

        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多