JB_User
JB_User

Reputation: 3267

Multiple Spinners in Android

I have two spinners in my Android Activity and I' trying to write the Java code that handles them with one method. I tried this, which is what I did with handling multiple buttons, but this doesn't work.

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    Log.d(TAG, "******** onItemSelected(" + position + ") **********");
    Log.d(TAG, "******** onItemSelected() View is (" + v + ") **********");
    switch(v.getId()) {
        case R.id.SpinnerTaxRate :
            ....
            break
        case R.id.SpinnerDiscount :
             ....
             break

The problem is that v.getId() isn't being set for the spinner. How can I correct this?

Here is the XML for one of the spinners (the other is very similar):

    <Spinner android:id="@+id/SpinnerTaxRate"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"/>

This is the code that registers the Spinner for the ItemSelectedListener.

    SpinnerTaxRate = (Spinner) findViewById(R.id.SpinnerTaxRate);
    SpinnerTaxRate.setOnItemSelectedListener(this);

Upvotes: 1

Views: 281

Answers (3)

stinepike
stinepike

Reputation: 54672

switch(parent.getId()) {
    case R.id.SpinnerTaxRate:
        ....
        break

use this instead. You have used wrong id

the parent.getId() returns the corresponding Id of view that you set in the layout. Here you have used SpinnerTaxRate as id but you r comaparing with spinner1. So compare the correct id.

EDIT I overlooked the key part .. instead of v.getId() use parent.getID(). parent. Here

parent  The AdapterView where the selection happened
v       The view within the AdapterView that was clicked

Upvotes: 2

Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

The id of the spinner does not correspond to the switch statement

<Spinner android:id="@+id/Spinner1" <-----
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"/>

This would cause your switch statement to execute.

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    Log.d(TAG, "******** onItemSelected(" + position + ") **********");
    Log.d(TAG, "******** onItemSelected() View is (" + v + ") **********");
    switch(parent.getId()) {
        case R.id.Spinner1 : <---
            ....
            break
        case R.id.Spinner2 :
             ....
             break

Upvotes: 0

codeMagic
codeMagic

Reputation: 44571

The problem is that you are checking against v which is the TextView used inside the Spinner. You want to use the AdapterView<?>, or parent which is the actual `Spinner

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
Log.d(TAG, "******** onItemSelected(" + position + ") **********");
Log.d(TAG, "******** onItemSelected() View is (" + v + ") **********");
switch(parent.getId()) {
    case R.id.SpinnerTaxRate :
        ....
        break
    case R.id.SpinnerDiscount :
         ....
         break

Upvotes: 4

Related Questions