Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4106026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:19:24+00:00 2026-05-20T21:19:24+00:00

In my activity, I have a Spinner and a ListView. The Spinner is used

  • 0

In my activity, I have a Spinner and a ListView. The Spinner is used to filter the items in the ListView. When the user clicks on a list item, another activity is called, which shows the details of the list item. Then, from the details activity, the user presses the back button, and the spinner (thus the the list) gets reset to its initial value.

This is due to the fact that I populate the spinner’s adapter, and declare its onItemSelectedListener, into the onStart() method, which is called again when the user presses the back button. Also the onItemSelectedListener is actually called once by the onStart() method.

How do I avoid this behavior? I guess I should move the listener and the adapter somewhere else, but where?

Here is an overview of the class

public static ArrayList<String> years;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.archive);

    // populate years array here
}

@Override
protected void onStart()
{
    super.onStart();

    Spinner spinner = (Spinner) findViewById(R.id.spinner_anni);

    // create spinner adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, ArchiveActivity.years);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    // create spinner listener
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parentView,
                View selectedItemView, int position, long id)
        {
            filterList(ArchiveActivity.years.get(position));
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView)
        {
            return;
        }
    });

    // create list listener
    ListView list = (ListView) findViewById(R.id.list_archive);
    list.setOnItemClickListener(this);
}

private void filterList(String year)
{
    // filter list data here
}

@Override
public void onItemClick(AdapterView<?> list, View item, int pos, long id)
{
    // call detail activity for list item that's been pressed
    Intent i = new Intent(this, DetailActivity.class);
    startActivity(i);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:19:24+00:00Added an answer on May 20, 2026 at 9:19 pm

    Normally you initiated the spinner and the adapter in the onCreate() method. Furthermore you have to save the status of you spinner and recreate it when the activity comes back to the foreground. Check this question on how to save the application state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView in which each List item has a Spinner . I
I have an activity which shows a Spinner (selection of category of items), a
I have an activity with a spinner which loads a simpleCursorAdaptor. I call another
I have a view with a spinner. The activity starts another acvitity with a
In my main activity I have a spinner which contains three categories(All,Horizontal and Vertical),
i Have a Spinner on my activity that contains a list of String, it
In one of my Activity, i have used the Spinner . i populate it
I have a spinner in my activity with a list of options, When the
On opening the activity, the user chooses an option from a spinner. I have
I have exported an app with 3 activities (main, splash and another activity called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.