Java 在 Android Spinner 中设置值和显示文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2942132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:42:52  来源:igfitidea点击:

Setting values and display Text in Android Spinner

javaandroiddrop-down-menuspinner

提问by kaibuki

I need help in setting up value and display text in spinner. as per now I am populating my spinner by array adapter e.g

我需要在微调器中设置值和显示文本方面的帮助。现在我正在通过阵列适配器填充我的微调器,例如

mySpinner.setAdapter(myAdapter);

and as far as I know after doing this the display text and the value of spinner at same position is same. The other attribute that I can get from spinner is the position on the item.

据我所知,在执行此操作后,同一位置的显示文本和微调器的值是相同的。我可以从微调器获得的另一个属性是项目上的位置。

now in my case I want to make spinner like the drop down box, which we have in .NET.

现在在我的情况下,我想让 Spinner 像下拉框一样,我们在 .NET 中有。

which holds a text and value.

其中包含文本和值。

where as text is displayed and value is at back end. so if I change drop down box , I can either use its selected text or value. but its not happening in android spinner case.

其中显示文本,值在后端。因此,如果我更改下拉框,则可以使用其选定的文本或值。但它不会发生在 android spinner 案例中。

For Example:

例如:

Text Value
Cat 10
Mountain 5
Stone 9
Fish 14
River 13
Loin 17

文本值
猫 10
山 5
石头 9
鱼 14
河 13
腰 17

so from above array I am only displaying non-living objects text, and what i want is that when user select them I get there value i.e. like when Mountain selected i get 5

所以从上面的阵列中,我只是显示非活着的对象文本,以及我想要的是,当用户选择它们时,我会得到那么有价值,即当山上选择时,我得到5

I hope this example made my question a bit more clear...

我希望这个例子能让我的问题更清楚一点......

thankx

谢谢

采纳答案by CommonsWare

Step #1: Create a data model for your text and value pair

第 1 步:为文本和值对创建数据模型

Step #2: Extend BaseAdapteror ArrayAdapterto wrap around your data model, overriding getView()to return whatever you want to display in the Spinneritself

第 2 步:扩展BaseAdapterArrayAdapter环绕您的数据模型,覆盖getView()以返回您想要在其Spinner自身中显示的任何内容

Step #3: On a selection event, use the position to find the corresponding object in your data model, and look up the value

步骤 #3:在选择事件上,使用位置在数据模型中找到相应的对象,并查找值

If all you want to display in the Spinneris just a text representation of each element in the data model, you can skip the custom Adapter-- just use ArrayAdapter<YourModelClass>and override toString()on YourModelClassto return whatever display text you want.

如果你想在显示Spinner只是在数据模型中的每个元素的文本表示,可以跳过自定义Adapter-只要使用ArrayAdapter<YourModelClass>,并覆盖toString()YourModelClass任何你想要显示文本的回报。

回答by alun

If you dont want to go through creating your own adapter-class you can instead wrap you data up in something that has toString returning what you want to show in the spinner. I use a class like this:

如果您不想创建自己的适配器类,则可以将数据包装在具有 toString 的内容中,返回您想要在微调器中显示的内容。我使用这样的类:

public class TextItemPair<T> {
    private String text;
    private T item;
    public TextItemPair(String text, T item) {
            this.text = text;
            this.item = item;
    }
    public String getText() {
        return text;
    }
    public T getItem() {
        return item;
    }
    @Override
    public String toString() {
        return getText();
    }
}

And when the spinner changes:

当微调器改变时:

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    YourTypeHere t = ((TextItemPair<YourTypeHere>)spinner.getItemAtPosition(position)).getItem();
}