title: How to save spinner and reload EditText input from spinner click
body: <p>I've create An EditText to insert my name and one to intert my surname. When i click add it must save my name into a spinner and when i click on it, i want to appear my name and my surname into edittext. I've create this code but it save it and reload only the name into EditText Name and EditText Surname..how can I do? is possible to reload also the surname only clicking the name spinner? this is my code:</p>
  
  <pre><code>    // Spinner element
  Spinner spinner;
  
  // Add button
  Button btnAdd, btnReset;
  
  // Input text
  EditText inputLabel, surname, cf, age;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      // Spinner element
      spinner = (Spinner) findViewById(R.id.spinner);
  
      // add button
      btnAdd = (Button) findViewById(R.id.btn_add);
  
      //reset Button
      btnReset =(Button) findViewById (R.id.btn_reset);
  
      // new label input field
      inputLabel = (EditText) findViewById(R.id.editTextName);
      surname = (EditText) findViewById(R.id.editTextSurName);
      cf = (EditText) findViewById(R.id.editTextCodicefiscale);
      age = (EditText) findViewById(R.id.editTextAge);
  
  
      // Spinner click listener
      spinner.setOnItemSelectedListener(this);
  
      // Loading spinner data from database
      loadSpinnerData();
  
      /**
       * Add new label button click listener
       * */
      btnAdd.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View arg0) {
              String label = inputLabel.getText().toString();
              String label2 = surname.getText().toString();
  
              if (label.trim().length() &gt; 0) {
                  // database handler
                  DatabaseHandler db = new DatabaseHandler(
                          getApplicationContext());
  
                  // inserting new label into database
                  db.insertLabel(label);
                  db.insertLabel2(label2);
  
  
                  // making input filed text to blank
                  inputLabel.setText("");
                  surname.setText("");
  
                  // Hiding the keyboard
                  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(inputLabel.getWindowToken(), 0);
                  imm.hideSoftInputFromWindow(surname.getWindowToken(), 0);
  
                  // loading spinner with newly added data
                  loadSpinnerData();
              } else {
                  Toast.makeText(getApplicationContext(), "Please enter label name",
                          Toast.LENGTH_SHORT).show();
              }
  
          }
      });
  
  
      btnReset.setOnClickListener(new View.OnClickListener() {
  
          public void onClick(View v) {
              // TODO Auto-generated method stub
              inputLabel.setText("");
              surname.setText("");
              cf.setText("");
              age.setText("");
          }
      });
  }
  
  /**
   * Function to load the spinner data from SQLite database
   * */
  private void loadSpinnerData() {
      // database handler
      DatabaseHandler db = new DatabaseHandler(getApplicationContext());
  
      // Spinner Drop down elements
      List&lt;String&gt; lables = db.getAllLabels();
  
      // Creating adapter for spinner
      ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(this,
              android.R.layout.simple_spinner_item, lables);
  
      // Drop down layout style - list view with radio button
      dataAdapter
              .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  
      // attaching data adapter to spinner
      spinner.setAdapter(dataAdapter);
  }
  
  public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position,
          long id) {
      // On selecting a spinner item
      String label = parent.getItemAtPosition(position).toString();
      String label2 =  parent.getItemAtPosition(position).toString();
      inputLabel.setText(label);
      surname.setText(label2);
      // Showing selected spinner item
      Toast.makeText(parent.getContext(), "You selected: " + label,
              Toast.LENGTH_LONG).show();
  
  }
  
  public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
      // TODO Auto-generated method stub
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and this is my DatabaseHandler:</p>
  
  <pre><code> // Database Version
  private static final int DATABASE_VERSION = 1;
  
  // Database Name
  private static final String DATABASE_NAME = "spinnerExample";
  
  // Labels table name
  private static final String TABLE_LABELS = "labels";
  
  // Labels Table Columns names
  private static final String KEY_ID = "id";
  private static final String KEY_NAME = "name";
  private static final String KEY_SURNAME = "surname";
  
  
  public DatabaseHandler(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }
  
  // Creating Tables
  @Override
  public void onCreate(SQLiteDatabase db) {
      // Category table create query
      String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS + "("
              + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + KEY_SURNAME + " TEXT)";
      db.execSQL(CREATE_CATEGORIES_TABLE);
  
  
  }
  
  // Upgrading database
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // Drop older table if existed
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);
  
      // Create tables again
      onCreate(db);
  }
  
  /**
   * Inserting new lable into lables table
   * */
  public void insertLabel(String label){
      SQLiteDatabase db = this.getWritableDatabase();
  
      ContentValues values = new ContentValues();
      values.put(KEY_NAME, label);
  
      // Inserting Row
      db.insert(TABLE_LABELS, null, values);
      db.close(); // Closing database connection
  }
  
  public void insertLabel2(String label2){
      SQLiteDatabase db = this.getWritableDatabase();
  
      ContentValues values2 = new ContentValues();
      values2.put(KEY_SURNAME, label2);
  
      // Inserting Row
      db.insert(TABLE_LABELS, null, values2);
      db.close(); // Closing database connection
  }
  
  /**
   * Getting all labels
   * returns list of labels
   * */
  public List&lt;String&gt; getAllLabels(){
      List&lt;String&gt; labels = new ArrayList&lt;String&gt;();
  
      // Select All Query
      String selectQuery = "SELECT  * FROM " + TABLE_LABELS;
  
      SQLiteDatabase db = this.getReadableDatabase();
      Cursor cursor = db.rawQuery(selectQuery, null);
  
      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {
              labels.add(cursor.getString(1));
          } while (cursor.moveToNext());
      }
  
      // closing connection
      cursor.close();
      db.close();
  
      // returning lables
      return labels;
  }
  </code></pre>
  
  <p>}</p>