旋转器未初始化以更正阵列

时间:2012-06-08 18:14:45

标签: android spinner android-spinner

我有一个带有三个微调器的应用程序,它们有唯一的ID,我正在尝试使用以下代码来初始化它们:

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

    Spinner spinner2 = (Spinner) findViewById(R.id.spinReroll);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.reroll_nums, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter);

    Spinner spinner3 = (Spinner) findViewById(R.id.spinTarget);
    ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.target_nums, android.R.layout.simple_spinner_item);
    adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner3.setAdapter(adapter);
    spinTarget.setSelection(3);

他们初始化并充满了值,当然,但是查看strings.xml中的数组:

<string-array name="reroll_nums">
    <item>10</item>
    <item>9</item>
    <item>8</item>
    <item>7</item>
    <item>6</item>
    <item>5</item>
    <item >4</item>
    <item >3</item>
    <item >2</item>
</string-array>
<string-array name="double_nums">
    <item>10</item>
    <item>9</item>
    <item>8</item>
    <item>7</item>
    <item>6</item>
    <item>5</item>
    <item >4</item>
    <item >3</item>
    <item >2</item>
    <item >1</item>
</string-array>
<string-array name="target_nums">
    <item>10</item>
    <item>9</item>
    <item>8</item>
    <item>7</item>
    <item>6</item>
    <item>5</item>
    <item >4</item>
    <item >3</item>
    <item >2</item>
    <item >1</item>
</string-array>

Spinner#2(spinReroll)应该具有2-10的值,而其他值应该具有值1-10,但是当我在我的虚拟设备中运行此应用程序时,所有三个微调器都有1-10个值,这是不好的在spinReroll中选择1in会创建一个无限循环:P

我做错了什么?

1 个答案:

答案 0 :(得分:3)

 spinner2.setAdapter(adapter2); 
                            ^^^
 spinner3.setAdapter(adapter3);
                            ^^^

您先设置adapter而不是adapter2,还要设置spinner3。