title: Dynamically creating a spinner and setting its Value from SQLite
body: <p><strong>Found my problem check under "Update" for the code</strong></p>
  
  <p>Okay, I have a problem loading the right value from Sqlite into my Spinner. Here is how my app is constructed</p>
  
  <p>my onCreate() sets up a spinner called spinTypes by:</p>
  
  <pre><code>public class MyClass extends Activity{
   // local members . . . 
       .
       .
       .
   //spinner 
   private Spinner spinTypes, spinNames = null;
  
   // string array decl.
   private String [] types, names1, names2, names3 = null;
  
   // array adapters for string arrays
   private ArrayAdapter&lt;String&gt; typesAdapter, names1Adapter,
                               names2Adapter, names3Adapter;
  
   // create a Dbhelper object to access the db
   private DbHelper mdbHelper = null;
  
  // on create
   @Override
   public void onCreate(Bundle savedIstanceState){
    // call to super class
    // set my content view
    // instantiate all members 
  
    // Dbhelper object to access the db
    mdbHelper = new DbHelper(getBaseContext());
  
    // spinners
    spinTypes = (Spinner) findViewById(R.id.spin_types);
    spinNames = (Spinner) findViewById(R.id.spin_names);
  
   // get string arrays from resources and create a ArrayAdapter&lt;string&gt;
   types = getResources().getStringArray(R.array.types);
  
   typesAdapter = new ArrayAdapter&lt;String&gt;(getBaseContext(),
                                          android.R.layout.simple_spinner_item, types);
   typesAdapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
   // want to create a dynamic spinner based on these arrays
   names1 = getResources().getStringArray(R.array.names_1); 
   names2 = getResources().getStringArray(R.array.names_2);
   names3 = getResources().getStringArray(R.array.names_3);
  
   // do this for all names++ arrays
   names1Adapter = new ArrayAdapter&lt;String&gt;(getBaseContext(),
                                          android.R.layout.simple_spinner_item, names1);
   names1Adapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
   // etc . . . for the other two
  
   // set the adapter for the types spinner
   spinTypes.setAdapter(typesAdapter);
  
  }
  </code></pre>
  
  <p>I have a creation method for inserting all my data into the db that works fine, my problem is when i want to try and populate the spinners for editing an entry.</p>
  
  <p>The app works as so: you press an add button to popup a dialog that creates a list item in the calling view. I use two spinners to create it, one for the type which dynamically populates the second names spinner based on the type.</p>
  
  <p>when i access the database object i return the strings from the spinner but for some reason when i create my if statement checking the position of the string in the array the second spinner sets position 0 every time.</p>
  
  <p>the method including the if statement i mentioned above is as so:</p>
  
  <pre><code>public void populate(){
    mdbHelper.open();
    // if there is an item create a cursor rowId != null
    Cursor mTypes = mdbHelper.fetchItem(rowId);
     if(mTypes.moveToFirst()){
     String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE));
     String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME));
  
    int t = spinTypes.getPosition(tType);
    spinTypes.setSelection(t);
  
    // ** this does not seem to do the job i want it to**
    if(t == 0){
      spinNames.setAdapter(names1Adapter); // set the adapter based on t value
      int n1 = names1Adapter.getPosition(tName); // use name return from db to get pos
      spinNames.setSelection(n1);            // set the position
    }else if(t ==1){
      spinNames.setAdapter(names2Adapter);
      int n2 = names1Adapter.getPosition(tName);
      spinNames.setSelection(n2);
    }else{
      spinNames.setAdapter(names3Adapter);
      int n3 = names3Adapter.getPosition(tName);
      spinNames.setSelection(n3);
    }
  
  }// end populate
  
  } // end class
  </code></pre>
  
  <p>The spinners all work but they do not end up on the value with tName i am returning.</p>
  
  <p>Can anyone help with this?</p>
  
  <p>** I am logging the name returned from the db and using it to find the index of the item in my error and i return the correct value, yet the spinner still defaults to 0**</p>
  
  <p>Anyone know what my error may be? please</p>
  
  <p><strong>Update(final code posted below)</strong>
  I made quite a bit of changes to get this to work, in order to understand what i did you should read this whole post so you don't miss anything!</p>
  
  <p>added a variable before <code>onCreate()</code>
  <code>private int spinNamePos;</code></p>
  
  <p>added this line after my last line of the <code>onCreate()</code></p>
  
  <pre><code>// add a listener to the spinner
  spinTypes.setOnItemSelectedListener(TypesListener);
  </code></pre>
  
  <p>and created this listener as so</p>
  
  <pre><code>OnItemSelectedListener TypesListener = new OnItemSelectedListener(){
    public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id){
      // use the position of this spinner to set the second spinner
       switch(pos){
       case 0 :
          spinNames.setAdapter(names1Adapter);
          break;
        //etc. .  for the other two
       case 1:
          // etc
         break;
       }
       case 2:
          // etc
         break;
       }
       // created an int for the position of the second spinner before calling onCreate()
       // and use it to set this spinners position
       spinNames.setSelection(spinNamePos); 
   // this value is grabbed in onSavedInstanceState() and used to set the pos when onRestoreInstanceState() is called to handle orientation changes and activity paused
    }
   };
  </code></pre>
  
  <p>i then changed my <code>onPopulate()</code> to work as so calling a new method to set the second spinner's position</p>
  
  <pre><code> public void populate(){
    mdbHelper.open();
    // if there is an item create a cursor rowId != null
    Cursor mTypes = mdbHelper.fetchItem(rowId);
     if(mTypes.moveToFirst()){
     String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE));
     String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME));
  
    int t = spinTypes.getPosition(tType);
    spinTypes.setSelection(t);
  
    // call to the new method
    spinNamePos = setSpinNamesPos( t, tName);
    // set the position
    spinNames.setSelection(spinNamePos)
  
  
  }// end populate
  
  // new method to set the position when i want to update/change the list item
  
  private int setSpinNamesPos(int m, String name){
   int pos = 0;
   switch(m){
    case 0:
      pos = names1Adapter.getPosition(name);
      break;
    case 1:
      pos = names2Adapter.getPosition(name);
      break;
     case 2:
      pos = names3Adapter.getPosition(name);
      break;
    }
   return pos;
  }
  </code></pre>
  
  <p><strong>So happy to see this working any questions or comments are encouraged</strong></p>