在Android的微调设定值和显示文本 [英] Setting values and display Text in Android Spinner

查看:155
本文介绍了在Android的微调设定值和显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在的我来说,我想使微调像下拉框,这是我们在.NET。

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

持有文本和价值。

在这里为文本显示和价值是在后端。 所以,如果我改变下拉框中,我可以用它选定的文本或值。 但它不是发生在Android的微调情况。

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.

例如:

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

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

所以从上面的数组我只是显示非生命物体的文字,而我想的是,当用户选择他们我到那里的价值,即像在山中选择,我得到5

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

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

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

thankx

推荐答案

如果你不想去通过创建自己的适配器类,你可以,而不是包装你的数据在一些已的toString返回要在显示什么微调。我使用一类是这样的:

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:

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

这篇关于在Android的微调设定值和显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆