飞旋从strings.xml中阵列设置值 [英] Set Value of spinner from Array in strings.xml

查看:111
本文介绍了飞旋从strings.xml中阵列设置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个微调,使用来自strings.xml中的数组

I have a spinner, that uses an array from strings.xml

如果数组有5串(1,2,3,4,5),我想微调显示第二 字符串(2)为默认值,这可能吗? 我知道我可以重新安排串顺序,使第一个是2, 但这并不看起来非常好,如果微调对话框显示为(2,1,3,4,5)。

if the array has 5 strings (1,2,3,4,5), and i want the spinner to show second string (2) as default value, is this possible? I know i can re-arrange the strings order so that first one is 2, but this doesn't look very good if spinner dialog appears as (2,1,3,4,5).

抑或是阵列必须安装在我的活动中创建编程 然后用setPostion()? 我曾经试过,但创建阵列中的活动时,获得一个错误。 任何人都可以请给我如何创建阵列和使用的例子 它在微调()

Or does the array have to be created within my activity programatically and then use setPostion()? I have tried this, but get a fault when creating array in activity. Can anyone please give me an example of how to create array and use it in spinner()

我还搜查这里来寻找答案,但似乎无法找到我所需要的。

I have also searched on here for answers but cant seem to find what i need.

感谢您看......

Thanks for looking....

推荐答案

我建议你检查:<一href="http://developer.android.com/resources/tutorials/views/hello-spinner.html">http://developer.android.com/resources/tutorials/views/hello-spinner.html

您应该在活动创建一个ArrayAdapter。

You should create an ArrayAdapter in your Activity.

从上面的链接:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}

您可以使用

spinner.setSelection(adapter.getPosition(value)));

设置位置。

这篇关于飞旋从strings.xml中阵列设置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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