我正在开发这个应用程序,其中一个活动有两个微调器,并且都加载相同的条目,甚至我为每个Spinner定义了不同的条目。这很奇怪。
这是代码,
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>
我没有看到任何错误,即使我使用了不同的适配器,它也显示相同的值。
答案 0 :(得分: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
到错误的Spinner。糊。