简体   繁体   English

在没有hashmap结构的微调器中使用键和值

[英]Using key and value in spinner without hashmap structure

I have list of keys and values, key = is a String of id from table. 我有键和值列表,key =是表中的id字符串。 value = is a String of name, value =是一个名字的字符串,

I want to show inside spinner view only the vaulues without the Ids, but after the user cliked on some item I need to get the Id of the selected item, and using it in code. 我想在spinner视图中只显示没有Ids的vululues,但是在用户访问某个项目后我需要获取所选项目的Id,并在代码中使用它。 How can I implement this structure whitout using hashMap or many foreach statements? 如何使用hashMap或许多foreach语句实现此结构whitout?

Thank you, 谢谢,

click listener receives clicked view. click listener接收单击的视图。 Class view has support for 'ViewHolder' pattern (as in RecyclerView) where references to all widgets is stored in ViewHolder and can be accessed later. 类视图支持“ViewHolder”模式(如在RecyclerView中),其中对所有窗口小部件的引用存储在ViewHolder中,可以在以后访问。 You can use the same approach- create class, containing some info and reference to click listener then set it to you dropdown item on creation by View.setTag(). 您可以使用相同的方法 - 创建类,包含一些信息和对点击侦听器的引用,然后将其设置为View.setTag()创建的下拉项。 When item is selected, query this object and call runnable you want to execute - your action 选择项目后,查询此对象并调用要执行的runnable - 您的操作

Create a POJO class and pass an ArrayList of your custom objects to your spinner. 创建一个POJO类并将自定义对象的ArrayList传递给微调器。

class MyClass{
    private String id;
    private String key;

    public MyClass(String id, String value) {
        this.id = id;
        this.value = value;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    } 

    @Override
    public String toString() {
        return value;
    }   
}

Overriding toString() method is important. 覆盖toString()方法很重要。 That's what will be shown in spinner item. 这就是旋转器项目中显示的内容。

You can pass it to your ArrayAdapter like that: 你可以将它传递给你的ArrayAdapter

ArrayAdapter userAdapter = new ArrayAdapter(this, R.layout.spinner, myList);

When a user clicks on an item, getSelectedItem() method will give you object. 当用户单击某个项时, getSelectedItem()方法将为您提供对象。 You can get the id from there. 你可以从那里得到id

From the docs : 来自文档

A concrete BaseAdapter that is backed by an array of arbitrary objects. 由任意对象数组支持的具体BaseAdapter。 By default this class expects that the provided resource id references a single TextView. 默认情况下,此类期望提供的资源ID引用单个TextView。 If you want to use a more complex layout, use the constructors that also takes a field id. 如果要使用更复杂的布局,请使用也带有字段ID的构造函数。 That field id should reference a TextView in the larger layout resource. 该字段id应引用较大布局资源中的TextView。

However the TextView is referenced, it will be filled with the toString() of each object in the array. 但是引用了TextView,它将填充数组中每个对象的toString()。 You can add lists or arrays of custom objects. 您可以添加自定义对象的列表或数组。 Override the toString() method of your objects to determine what text will be displayed for the item in the list. 覆盖对象的toString()方法,以确定将为列表中的项显示的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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