J.Doe
J.Doe

Reputation: 31

How to fix automatic view another activity on spinner onItemSelected methods

I'm working on simple android project that require spinner widget in android studio. what i want to do is, when a user select choices from the spinner it will open another activity. while i'm in the middle of the coding. I decided to use switch case condition with Intent. The problem is whenever i run the application it will automatically go to the specific activity location that i declare. Even though I didn't select any choice on spinner. Note: log cat doesn't show any error

Your help is very much appreciated to beginner like me.

public class CustomOnItemSelectedListener extends Activity implements
        OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos,
                               long id) {

        // **************************** below here is where I start the new activity
        switch (pos) {
            case 0 :
                Intent i = new Intent(app.this, home.class);
                app.this.startActivity(i);
                break;

            case 1 :
                //Intent intent = new Intent(app.this, about.class);
                //app.this.startActivity(intent);
                break;

            case 2 :
                //Intent intent1 = new Intent(app.this, home.class);
                //app.this.startActivity(intent1);
                break;

        }
        // **************************** above here is where I start the new activity
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

Upvotes: 0

Views: 212

Answers (2)

Firerazzer
Firerazzer

Reputation: 192

When the application is created by default the first option is selected in your spinner and your listener is called. To avoid this you can use the solution of @Simon

or you can use a button to confirm your selection and change your activity when this button is pressed and not when an item is selected in your spinner.

UPDATE You can also populate the first entry of your spinner with a useless text which can be a title like "Select activity" and start your switch in the listener to 1 and not 0

Upvotes: 0

Simon Andersson
Simon Andersson

Reputation: 791

Try this

Declare an int in your class, e.g. before onCreate(), then in your onCreate() you assign it to 0. Use this variable to check if its bigger than 0 when selecting something with your spinner, example below.

public class ExampleActivity extends AppCompatActivity {
private int spinnerCheck;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mSpinnerCheck = 0;

    mMySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            int itemId = (int) id;

            // For some reason this method is called during initialization, so increment counter once to prevent it from auto selecting first item when loading view
            spinnerCheck += 1;

            if (spinnerCheck > 1) {
                switch (pos) {
                    case 0:
                        Intent i = new Intent(app.this, home.class);
                        app.this.startActivity(i);
                        break;

                    case 1:
                        //Intent intent = new Intent(app.this, about.class);
                        //app.this.startActivity(intent);
                        break;

                    case 2:
                        //Intent intent1 = new Intent(app.this, home.class);
                        //app.this.startActivity(intent1);
                        break;

                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
   }
}

For some reason it selects the first item once created, not sure why, but this should work.

Upvotes: 1

Related Questions