2012-06-26 49 views
1

我正在开发这个应用程序,其中一个活动有两个spinners,并且都加载相同的条目,即使我为每个Spinner定义了不同的条目。这是相当奇怪的事情。两个spinners显示相同的条目

下面是代码,

main.xml中布局(部分0F布局)

<TableRow 
     android:id="@+id/settings_color_row" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="10dp" > 

     <TextView 
      android:id="@+id/settings_color_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_span="0" 
      android:gravity="left" 
      android:paddingLeft="7dp" 
      android:paddingTop="13dp" 
      android:text="Color" 
      android:textSize="18dp" 
      android:textStyle="bold" 
      android:typeface="serif" > 
     </TextView> 

     <Spinner 
      android:id="@+id/settings_color_spinner" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/settings_background_row" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="10dp" > 

     <TextView 
      android:id="@+id/settings_background_text" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_span="0" 
      android:gravity="left" 
      android:paddingLeft="7dp" 
      android:paddingTop="13dp" 
      android:text="Skin" 
      android:textSize="18dp" 
      android:textStyle="bold" 
      android:typeface="serif" > 
     </TextView> 

     <Spinner 
      android:id="@+id/settings_background_spinner" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      /> 
    </TableRow> 

Settings.java(活动)

colors_spinner = (Spinner)findViewById(R.id.settings_color_spinner); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.colors_array, android.R.layout.simple_spinner_item); 
    // Specify the layout to use when the list of choices appears 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    // Apply the adapter to the spinner 
    colors_spinner.setAdapter(adapter); 

    skin_spinner = (Spinner)findViewById(R.id.settings_background_spinner); 
    ArrayAdapter<CharSequence> skin_adapter = ArrayAdapter.createFromResource(this, 
      R.array.background_array, android.R.layout.simple_spinner_item); 
    // Specify the layout to use when the list of choices appears 
    skin_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    // Apply the adapter to the spinner 
    colors_spinner.setAdapter(skin_adapter); 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<string name="hello">Hello World, Settings!</string> 
<string name="app_name">Shared Preferences Test</string> 

<string-array name="colors_array"> 
    <item>White</item> 
    <item>Red</item> 
    <item>Blue</item> 
    <item>Pink</item> 
</string-array> 

<string-array name="background_array"> 
    <item>Red-Nosed Reindeer</item> 
    <item>Snowman</item> 
</string-array> 

</resources> 

我没有看到任何呃即使我使用了不同的适配器,它也表现出相同的值。

enter image description here

回答

4

尝试改变这最后一行:

skin_spinner = (Spinner)findViewById(R.id.settings_background_spinner); 
ArrayAdapter<CharSequence> skin_adapter = ArrayAdapter.createFromResource(this, 
     R.array.background_array, android.R.layout.simple_spinner_item); 
// Specify the layout to use when the list of choices appears 
skin_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
// Apply the adapter to the spinner 
colors_spinner.setAdapter(skin_adapter); 

这样:

skin_spinner.setAdapter(skin_adapter); // not colors_spinner 

我相信你只是没有注意到你正在设置skin_adapter到错误微调后&粘贴。

+1

哦,我是多么愚蠢!对不起,我正在研究这个问题好几个小时了,我错过了,你能相信我挣扎了一个多小时,试图弄清楚出了什么问题。 –

+0

没有麻烦,我们都在那里。 – Sam