title: Android: Spinners within a ListView loose their values when I add dynamically new ListView entries
body: <p>My activity contains a button and a ListView. The ListView contains a Spinner and an EditText view. I use the button each time I want to insert a new row entry in my Activity's ListView.</p>
  
  <p>I have followed the instructions of previous stackoverflow threads like this one here: <a href="https://stackoverflow.com/questions/3952375/android-listview-with-spinner-and-a-checkbox">Android Listview with spinner and a checkbox</a> on how to populate ListViews with focusable objects like Spinners.</p>
  
  <p>My problem is that each time I dynamically add a new ListView entry in the ListView, the Spinner value of the previous ListView entry is lost (actuall the Spinner returns to its default setting). Say for simplicity that my Spinners are populated with the following data:</p>
  
  <pre><code> String spinner_data[] = {"apple", "banana", "pear", "watermelon", "strawberry"};
  </code></pre>
  
  <p>For example, if I select my first ListView's Spinner value to be "pear" and then I add a new ListView entry with my Button, the "pear" entry disappears from the 1st ListView Spinner and the default value "apple" appears). </p>
  
  <p>Any help is appreciated!</p>
  
  <p>This is my activity:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      lv = (ListView) findViewById(R.id.lv);       
      da = new DataAdapter(this, new ArrayList&lt;RawData&gt;());
      lv.setAdapter(da);
  
      btn_new = (Button)findViewById(R.id.btn_new);
  
      btn_new.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              da.add(new RawData(this));
                        da.notifyDataSetChanged();
          }
      });
  
  }
  </code></pre>
  
  <p>The RawData class is this one:</p>
  
  <pre><code> public class RawData {
  private int selected_position;
  private ArrayAdapter&lt;CharSequence&gt; adapter;
  
  public RawData(Context context)
  {
      adapter = ArrayAdapter.createFromResource(context, R.array.data, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  }
  
  public ArrayAdapter&lt;CharSequence&gt; getAdapter() 
  {         
      return adapter;     
  }
  
  /**
   * get adapter's item text from selected position
   * @return
   */
  public String getText() 
  {         
      return (String) adapter.getItem(selected_position);     
  }
  
  public int getSelected() 
  {   
      return selected_position;     
  }
  
  public void setSelected(int selected) 
  {         
      this.selected_position = selected;     
  }
   }
  </code></pre>
  
  <p>The DataArrayAdapter is the following:</p>
  
  <pre><code> public class DataArrayAdapter extends ArrayAdapter&lt;RawData&gt; {
  private Activity myContext;
  //private final List&lt;RawData&gt; list;
  
  public DataArrayAdapter(Activity context, List&lt;RawData&gt; list) 
  {         
      super(context, R.layout.row_view, list);         
      myContext = context;
  } 
  
  static class ViewHolder
  {
      protected RawData data;
      protected Spinner spn;
      protected EditText edt;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
      View view = null;
      if ( convertView == null )
      {
          LayoutInflater inflator = myContext.getLayoutInflater();             
          view = inflator.inflate(R.layout.row_view, null);
  
          final ViewHolder viewHolder = new ViewHolder(); 
  
          viewHolder.edt = (EditText)view.findViewById(R.id.edt);
          viewHolder.data = new RawData(myContext);
          viewHolder.spn = (Spinner)view.findViewById(R.id.spn);
          viewHolder.spn.setAdapter(viewHolder.data.getAdapter());
  
      viewHolder.spn.setOnItemSelectedListener(new OnItemSelectedListener() {
  
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; arg0, View arg1,
                      int arg2_position, long arg3) {
                  // TODO Auto-generated method stub
      viewHolder.data.setSelected(arg2_position);
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
                  // TODO Auto-generated method stub
  
              }
          });
  
          // Update the TextView to reflect what's in the Spinner
          view.setTag(viewHolder);
      }
      else
      {
          view = convertView;
      }
  
      // This is what gets called every time the ListView refreshes
      ViewHolder holder = (ViewHolder) view.getTag();
  
                 holder.spn.setSelection(getItem(position).getSelected());
  
      return view;
  }
  
   }
  </code></pre>