简体   繁体   English

设置值并在Android Spinner中显示文本

[英]Setting values and display Text in Android Spinner

I need help in setting up value and display text in spinner. 我需要帮助设置值并在微调器中显示文本。 as per now I am populating my spinner by array adapter eg 按照现在我通过数组适配器填充我的微调器,例如

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. 现在在我的情况下,我想制作像我们在.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 猫10
Mountain 5 山5
Stone 9 石头9
Fish 14 鱼14
River 13 河13
Loin 17 腰部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 ie like when Mountain selected i get 5 所以从上面的数组我只显示非生物对象文本,我想要的是,当用户选择它们时,我得到了值,就像当山选择我得到5

I hope this example made my question a bit more clear... 我希望这个例子让我的问题更加明确......

thankx thankx

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. 如果你不想创建自己的适配器类,你可以将数据包装成toString返回你想要在微调器中显示的内容。 I use a class like this: 我使用这样的类:

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

Step #1: Create a data model for your text and value pair 步骤1:为您的文本和值对创建数据模型

Step #2: Extend BaseAdapter or ArrayAdapter to wrap around your data model, overriding getView() to return whatever you want to display in the Spinner itself 步骤#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 Spinner is 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 YourModelClass to return whatever display text you want. 如果你想在显示Spinner只是在数据模型中的每个元素的文本表示,可以跳过自定义Adapter -只需使用ArrayAdapter<YourModelClass>并重写toString()YourModelClass返回任何你想要的显示文本。

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

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