Isuru Madusanka
Isuru Madusanka

Reputation: 1397

Two spinners show same entries

I am developing this app and one of an activity has two spinners, and both loads same entries, even I defined different entries for each Spinner. This is rather weird thing.

Here is the code,

main.xml layout(part 0f layout)

<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 (Activity)

    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>

I don't see any errors and it just show same values even though I used different adapters.

enter image description here

Upvotes: 1

Views: 740

Answers (1)

Sam
Sam

Reputation: 86948

Try changing the last line of this:

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

to this:

skin_spinner.setAdapter(skin_adapter); // not colors_spinner

I believe you just didn't notice that you are setting skin_adapter to the wrong Spinner after a cut & paste.

Upvotes: 4

Related Questions