title: How do I preserve the state of a selected spinner/dropdown item on orientation change?
body: <p>I am using a spinner dropdown in my code , in which I have 4 to 5 dynamically populated values, say if I have "apples" set to default and I select "oranges" from the dropdown and rotate my screen to landscape from portrait, it goes back to default "apples" along with the view associated with it.How do I save the state such that when I select "oranges" and rotate to landscape, it populates the selected value/remains in the same selected state and keeps the view intact/populates the view that was selected in the portrait mode corresponding to the selected value. Here's the adapter code I use for the same:</p>
  
  <pre><code>public class MarketsSpinnerAdapter extends CustomRowAdapter&lt;AdapterRow&gt; {
  
  
      private List&lt;AdapterRow&gt; mRenderList;
  
      public MarketsSpinnerAdapter(final Context context, final List&lt;AdapterRow&gt; renderList) {
          super(context);
  
  
          mRenderList = new ArrayList&lt;AdapterRow&gt;();
          mRenderList.addAll(renderList);
      }
  
      @Override
      protected void setEntries(final List&lt;AdapterRow&gt; renderList) {
          mRenderList = renderList;
      }
  
      @Override
      protected List&lt;AdapterRow&gt; getEntries() {
          return mRenderList;
      }
  
      @Override
      public View getDropDownView(final int position, final View convertView, final ViewGroup parent) {
          return getEntries().get(position).getDropDownView(mContext, convertView);
      }
  
  }
  </code></pre>
  
  <p>Corresponding usage in the respective fragment:</p>
  
  <pre><code> private void populateCategoryRows(final Cursor cursor) {
              mCategories.clear();
              mAllCategories.clear();
              cursor.moveToPosition(-1);
              Map&lt;String, String&gt; categoryParentNames = new HashMap&lt;String, String&gt;();
  
              int selectedPosition = 0;
              String previousHeader = "";
              String previousAllHeader = "";
  
              while (cursor.moveToNext()) {
                  final int categoryLevel = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.LEVEL));
                  final String categoryName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.NAME));
                  final String categoryDisplayName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.DISPLAY_NAME));
  
                  if (categoryLevel == 1) {
                      categoryParentNames.put(categoryName, categoryDisplayName);
                  }
              }
  
              cursor.moveToPosition(-1);
              while (cursor.moveToNext()) {
                  final int categoryLevel = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.LEVEL));
                  final boolean categoryIsDefault = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.IS_DEFAULT)) == 1;
                  final boolean categoryIsSelected = cursor.getInt(cursor.getColumnIndex(MarketsCategory.Columns.IS_SELECTED)) == 1;
                  final String categoryParent = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.PARENT));
                  final String categoryName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.NAME));
                  final String categoryDisplayName = cursor.getString(cursor.getColumnIndex(MarketsCategory.Columns.DISPLAY_NAME));
  
  
                  if (categoryLevel == 2 ) {
                      String categoryParentDisplayName = categoryParentNames.get(categoryParent);
                          if (!categoryParent.equals(previousHeader)) {
                              if (categoryIsSelected) {
  
                                  mCategories.add(new CategoryHeader(categoryParentDisplayName));
                                  previousHeader = categoryParent;
                              }
                          }
  
                          if (!categoryParent.equals(previousAllHeader)) {
                              mAllCategories.add(new CategoryHeader(categoryParentDisplayName));
                              previousAllHeader = categoryParent;
                          }
  
                          if (categoryIsSelected) {
                              mCategories.add(new SpinnerMarketCategoryRow(categoryName, categoryDisplayName, categoryParent));
                          }
                          mAllCategories.add(new MarketsCategoryCheckableRow(categoryName, categoryDisplayName, categoryIsSelected, categoryIsDefault));
  
                          if(categoryIsDefault){
                              selectedPosition = mCategories.size()-1;
                          }
                  }
              }
  
              mSpinnerAdapter = new MarketsSpinnerAdapter(Application.getAppContext(), mCategories);
              headerView.setSpinnerAdapter(mSpinnerAdapter);
              headerView.setSpinnerSelectedItemPosition(selectedPosition);
          }
          if (selectedItem instanceof SpinnerMarketCategoryRow) {
              selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position);
          } else {
              if (mSpinnerAdapter.getCount() - 1 &gt;= position + 1) {
                  selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position + 1);
              } else {
                  selectedCategory = (SpinnerMarketCategoryRow) mSpinnerAdapter.getItem(position - 1);
              }
          }
  
          final MarketsFragment parentFragment = (MarketsFragment) getParentFragment();
          parentFragment.onCategorySelected(selectedCategory.getCategoryName(), selectedCategory.getCategoryParentName());
      }
  @Override
      public void showResults(final Uri uri) {
          LayoutUtils.showResults(getView(), headerView.getSpinnerId());
          headerView.setVisibility(View.VISIBLE);
      }
  
      @Override
      public void showNoResults(final Uri uri) {
          final MarketsFragment parentFragment = (MarketsFragment) getParentFragment();
          parentFragment.hideSpinner();
          //LayoutUtils.showNoResult(getView(), headerView.getSpinnerId());
      }
  
      @Override
      public void onDismiss(DialogInterface dialog) {
          headerView.setSelected(false);
      }
      @Override
      public void onNothingSelected(IcsAdapterView&lt;?&gt; parent) {
      }
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Thanks!</p>