Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reset an Android spinner?

I am using a spinner as navigation for my app, and need to know how to reset it when the user uses the back button. Currently when a user selects a page and goes back, the spinner is on the previously selected bar, and not the current page. Here is my current code.

public void spinnerNavigation(){
    Spinner mySpinner = (Spinner) findViewById( R.id.spinner1);          
    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {
            if (i == 0) {
                // current page         
            } else if (i == 1) { // Second item 
                Intent myIntent = new Intent(getBaseContext(), LearnActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(myIntent);                                
            } else if (i == 2) { // Third item
                Intent myIntent = new Intent(getBaseContext(), QuizActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(myIntent);
            } else if (i == 3) { // Fourth item
                Intent myIntent = new Intent(getBaseContext(), ForumActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(myIntent);
            }
        }
        public void onNothingSelected(AdapterView<?> arg0) {
            // Do nothing
        }
    }); 
}
like image 938
hoss24 Avatar asked Jun 15 '26 07:06

hoss24


1 Answers

If you want to reset it to the first item:

mySpinner.setSelection(0); 
_____________________
like image 121
fweigl Avatar answered Jun 17 '26 22:06

fweigl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!