Spinner position incorrect after Activity resumes

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Spinner position incorrect after Activity resumes

Postby divestoclimb » Tue Jun 02, 2009 2:16 am

I built a class to help me build a Spinner in a consistent manner to display a list of categories. After the objects are initialized, my Activity calls a method initCatSpinner which builds the ArrayAdapter and finishes initializing my Spinner.

Called from onCreate and onActivityResult:
Code: Select all
mCategorySpinner = mCatSpinHelper.initCatSpinner(mCategoryId, catcursor);
Code: Select all
public Spinner initCatSpinner(long initial_category, Cursor all_categories) {
   LinkedList<CategorySpinnerItem> categories = new LinkedList<CategorySpinnerItem>();

   // Code to build this linked list from all_Categories is here, removed for brevity
   // Code adds an "All" item to the top, builds a list of categories using catcursor, then adds a "Edit Categories" option to the end of the list
   // CategorySpinnerItem is a simple object that lets me give each field a unique ID and reference it in listeners

   if(initial_category == ID_ALL) {
      mLastCategoryPosition=0;
   }

   // Build the spinner adapter and set up the Spinner
   ArrayAdapter<CategorySpinnerItem> spinadapter = new ArrayAdapter<CategorySpinnerItem>(mCtx,
         android.R.layout.simple_spinner_item,
         android.R.id.text1,
         categories);

   spinadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   mCatSpinner.setAdapter(spinadapter);
   mCatSpinner.setSelection(mLastCategoryPosition);
   mCatSpinner.setPrompt(mCtx.getResources().getString(R.string.choose_category));

   return mCatSpinner;
}

This works fine on two different activities when they are launched. One activity launches to show a list of all items in the category chosen by this spinner. From there I can launch another activity in a dialog to change the category of a selected item, but when I do this and return to the original activity the selected category is not visible on the spinner. I still see a list of the items in the proper category in my ListView, if I open the spinner the category that was originally selected is still selected (the green circle is next to the right item), but the spinner item that's displayed on the widget itself is always the first one in the list! It also happens if I switch to another Activity then come back to this one.

I've checked with Log output and mLastCategoryPosition IS being set correctly, so the setSelection call is passed the right data. I even tried calling SystemClock.sleep for a few hundred milliseconds thinking it might be a race condition, with no change. What's going on here? I'm using the 1.5r2 SDK.
divestoclimb
Developer
Developer
 
Posts: 33
Joined: Mon May 11, 2009 7:46 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests