title: spinner initialization in a custom view
body: <p>I try to make a custom view but I've got a problem with the spinner.
  When I launch the application everything is ok. Preferences are read and the spinner <code>setSelection</code> method work great (display picture is ok). 
  When I change the spinner value, there is no problem too : Preferences are saved and when I start the application the new choice is display.</p>
  
  <p>My problem comes when rotating the device, all my spinner value are reset to the last spinner value (vv2 in my code). I really don't understand...</p>
  
  <p><img src="https://i.stack.imgur.com/N2k94.png" alt="before rotating device"></p>
  
  <p><img src="https://i.stack.imgur.com/nIqMZ.png" alt="after rotating device"></p>
  
  <p>Here is my code :</p>
  
  <p>The widget_value_view.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;merge xmlns:android="http://schemas.android.com/apk/res/android" &gt;
      &lt;Spinner
          android:id="@+id/my_spinner"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:background="@android:color/transparent" /&gt;
  
      &lt;TextView
          android:id="@+id/my_value_tv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginLeft="15dp"
          android:layout_centerVertical="true"
          android:background="@android:color/transparent" /&gt;
  &lt;/merge&gt;
  </code></pre>
  
  <p>The ValueView.java class :</p>
  
  <pre><code>package com.example.spinnertest;
  import java.util.Arrays;
  import java.util.List;
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.util.AttributeSet;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.widget.LinearLayout;
  import android.widget.Spinner;
  import android.widget.TextView;
  
  public class ValueView extends LinearLayout {
  
      protected TextView mValueTv = null;
      protected Spinner mSpinner = null;
      protected List&lt;String&gt; mChoiceList;
  
      private MySpinnerAdapter mSpinnerAdapter;
  
      public ValueView(Context context) {
          super(context);
      }
  
      public ValueView(Context context, AttributeSet attributes){
          super(context,attributes);
  
          TypedArray a = context.getTheme().obtainStyledAttributes(attributes,R.styleable.ValueView,0, 0);
          try {
              String valueList = a.getString(R.styleable.ValueView_valueList);
              mChoiceList = Arrays.asList(valueList.split("\\|"));
          } finally {
              a.recycle();
          }
  
          setOrientation(LinearLayout.HORIZONTAL);
          setGravity(Gravity.CENTER_VERTICAL);
  
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          inflater.inflate(R.layout.widget_value_view, this, true);
  
          mSpinner = (Spinner) getChildAt(0);
          mValueTv = (TextView) getChildAt(1);
  
          mSpinnerAdapter = new MySpinnerAdapter(context, mChoiceList);
          mSpinner.setAdapter(mSpinnerAdapter);
      }
  
      /**
       * Change textview value
       */
      public void setValue(double value,String format){
          mValueTv.setText("todo");
          invalidate();
          requestLayout();
      }
  
      /**
       * Set the current item of the spinner
       * @param position of the item
       */
      public void setSelection(int position, boolean animate) {
          // mSpinnerAdapter.notifyDataSetChanged();
          mSpinner.setSelection(position,animate);
          // invalidate();
          // requestLayout();
      }
  
      public int getSelectedItemPosition() {
          return mSpinner.getSelectedItemPosition();
      }
  
  }
  </code></pre>
  
  <p>In this class I use a adapter define with 2 layout and one class :
  MySpinnerAdapter.java</p>
  
  <pre><code>package com.example.spinnertest;
  
  import java.util.List;
  import android.content.Context;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  public class MySpinnerAdapter extends BaseAdapter {
  
      public static final String ITEM_ALTITUDE = "altitude";
      public static final String ITEM_SPEED = "speed";
      public static final String ITEM_DISTANCE = "distance";
      private static final String LOG_TAG = "SPINNER_TEST";
  
      private Context mContext;
      private List&lt;String&gt; mChoiceList;
  
      public MySpinnerAdapter(Context context, List&lt;String&gt; values) {
          this.mContext = context;
          this.mChoiceList = values;
      }
  
      @Override
      public int getCount() {
          return mChoiceList.size();
      }
  
      @Override
      public Object getItem(int position) {
          return mChoiceList.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
              LayoutInflater layout = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = layout.inflate(R.layout.item_spinner_value, null);
          }
          ImageView imView = (ImageView) convertView.findViewById(R.id.item_spinner_value_iv);
          Log.d(LOG_TAG,"Spinner Adapter position = " + String.valueOf(position));
          imView.setImageResource(getSpinnerItemImage(position));
          return convertView;
      }
  
      @Override
      public View getDropDownView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
              LayoutInflater layout = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = layout.inflate(R.layout.item_spinner_list, null);
          }
          ImageView imView = (ImageView) convertView.findViewById(R.id.item_spinner_list_iv);
          TextView textView = (TextView) convertView.findViewById(R.id.item_spinner_list_tv);
          textView.setText(mChoiceList.get(position));
          imView.setImageResource(getSpinnerItemImageSmall(position));
          return convertView;
      }
  
      private int getSpinnerItemImageSmall(int position) {
          String item = mChoiceList.get(position);
          if (item.equals(ITEM_ALTITUDE)) {
              return R.drawable.altitude_small;
          } else if (item.equals(ITEM_DISTANCE)) {
              return R.drawable.distance_small;
          } else if (item.equals(ITEM_SPEED)) {
              return R.drawable.vitesse_small;
          } else {
              return R.drawable.date_small;
          }
      }
  
      private int getSpinnerItemImage(int position) {
          String item = mChoiceList.get(position);
          if (item.equals(ITEM_ALTITUDE)) {
              return R.drawable.altitude;
          } else if (item.equals(ITEM_DISTANCE)) {
              return R.drawable.distance;
          } else if (item.equals(ITEM_SPEED)) {
              return R.drawable.vitesse;
          } else {
              return R.drawable.date;
          }
      }
  }
  </code></pre>
  
  <p>and the 2 layout item_spinner_list.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;ImageView
          android:id="@+id/item_spinner_list_iv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:padding="5dp"
          android:src="@android:drawable/ic_menu_gallery" /&gt;
  
      &lt;TextView
          android:id="@+id/item_spinner_list_tv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:padding="5dp"
          android:layout_toRightOf="@+id/item_spinner_list_iv"
          android:text="Durée" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and item_spinner_value.xml :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/transparent" &gt;
  
      &lt;ImageView
          android:id="@+id/item_spinner_value_iv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:padding="5dp"
          android:src="@android:drawable/ic_menu_gallery" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and finally my MainActivity.java</p>
  
  <pre><code>package com.example.spinnertest;
  
  import android.app.Activity;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.preference.PreferenceManager;
  
  public class MainActivity extends Activity {
  
      private SharedPreferences mPref;
      private ValueView mVv1;
      private ValueView mVv2;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
  
          mVv1 = (ValueView)findViewById(R.id.value1);
          mVv2 = (ValueView)findViewById(R.id.value2);
  
          mVv1.setSelection(mPref.getInt("pref_val1", 0),false);
          mVv2.setSelection(mPref.getInt("pref_val2", 1),false);
      }
  
      @Override
      public void onPause() {
          SharedPreferences.Editor editor = mPref.edit();
          editor.putInt("pref_val1", mVv1.getSelectedItemPosition());
          editor.putInt("pref_val2", mVv2.getSelectedItemPosition());
          editor.commit();
          super.onPause();
      }
  }
  </code></pre>
  
  <p>and the layout :</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:custom="http://schemas.android.com/apk/res/com.example.spinnertest"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;com.example.spinnertest.ValueView
          android:id="@+id/value1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_margin="10dp"
          android:background="@android:color/transparent"
          custom:valueList="speed|altitude|distance" /&gt;
  
      &lt;com.example.spinnertest.ValueView
          android:id="@+id/value2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/value1"
          android:layout_margin="10dp"
          custom:valueList="speed|altitude|distance" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>