title: How to retrieve the int value of a selected item in a spinner?
body: <p>I'm making a relational database app in for android that displays an invoice based on the selected id I have used to populate a spinner. The spinner populates correctly, but I need to get the integer value of the id(not the index of the spinner) for the parameter of the <code>viewInvoice</code> method that holds the SQL. I have tried using the <code>getSelectedItem</code> method, but that doesn't seem to do what I need.</p>
  
  <p>Here is the code for the spinner:</p>
  
  <pre><code>package com.example.dh0020348.inventory;
  
  import android.app.AlertDialog;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Spinner;
  import android.widget.TextView;
  
  public class ViewAllCustomers extends AppCompatActivity {
  DbHelper db;
  TextView id, name, email, address, city, state, zip, phone, invoiceID;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_view_all_customers);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      db = new DbHelper(this);
  
      final Spinner spinner = (Spinner) findViewById(R.id.spinnerInvoiceID);
  
      DbBackend dbBackend = new DbBackend(ViewAllCustomers.this);
      String[] spinnerLists = dbBackend.getAllSpinnerContent();
  
      ArrayAdapter&lt;String&gt; spinnerAdapter = new ArrayAdapter&lt;String&gt;(ViewAllCustomers.this,android.R.layout.simple_spinner_item, spinnerLists);
      spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spinner.setAdapter(spinnerAdapter);
      spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
              double total;
              Cursor res = db.viewInvoice(Integer.parseInt(String.valueOf(spinner.getSelectedItem())));
              if (res.getCount() == 0) {
                  // show message
                  showMessage("Error", "Nothing found");
                  return;
              }
              StringBuffer buffer = new StringBuffer();
  
              buffer.append("Invoice ID: " + res.getString(0) + "\n");
              buffer.append("Date: " + res.getString(1) + "\n");
              buffer.append("Name: " + res.getString(2) + "\n");
              buffer.append("Street Address: " + res.getString(8) + "\n");
              buffer.append("City: " + res.getString(9) + "\n");
              buffer.append("State: " + res.getString(10) + "\n");
              buffer.append("Zip Code: " + res.getString(11) + "\n");
              buffer.append("Phone: " + res.getString(12) + "\n");
              buffer.append("Item: " + res.getString(4) + "\n");
              buffer.append("Size: " + res.getString(5) + "\n");
              buffer.append(String.format("Price Per Item: %.2f", Double.parseDouble(res.getString(7))) + "\n");
              buffer.append("Quantity: " + res.getString(6) + "\n");
              total = Double.parseDouble(res.getString(7)) * Double.parseDouble(res.getString(6));
              buffer.append(String.format("Total Price: %.2f", total));
  
              // show all data
              showMessage("Data", buffer.toString());
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
          }
      });
  
      id = (TextView) findViewById(R.id.lblCustomerID);
      name = (TextView) findViewById(R.id.lblCustomerName);
      email = (TextView) findViewById(R.id.lblCustomerEmail);
      address = (TextView) findViewById(R.id.lblCustomerAddress);
      city = (TextView) findViewById(R.id.lblCity);
      state = (TextView) findViewById(R.id.lblState);
      zip = (TextView) findViewById(R.id.lblZip);
      phone = (TextView) findViewById(R.id.lblPhone);
      invoiceID = (TextView) findViewById(R.id.lblInvoice);
  
      ViewAllCustomers();
  //        ViewCustomerInvoices();
      getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
  }
  
  //display the StringBuffer message
  public void showMessage(String title, String Message){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setCancelable(true);
      builder.setTitle(title);
      builder.setMessage(Message);
      builder.show();
  }
  
  public void ViewAllCustomers(){
  
      Cursor res = db.viewAllCustomers();
      while (res.moveToNext()) {
          id.setText("ID: " + res.getString(0));
          name.setText("Name: " + res.getString(1));
          email.setText("Email: " + res.getString(2));
          address.setText("Address: " + res.getString(3));
          city.setText("City: " + res.getString(4));
          state.setText("State: " + res.getString(5));
          zip.setText("Zip: " + res.getString(6));
          phone.setText("Phone: " + res.getString(7));
          invoiceID.setText("Invoice ID:");
      }
  }
  }
  </code></pre>
  
  <p>And here is the method with the query in my <code>DbHelper</code> class:</p>
  
  <pre><code> //view a single saved invoice
  public Cursor viewInvoice(int id){
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor res = db.rawQuery("SELECT ord." + INVOICE_ID + ", " + INVOICE_DATE + ", " + C_CUSTOMER_NAME + ", cus."
              + C_CUSTOMER_ID + ", inventory." + INVENTORY_ITEM + ", " + ORDERED_SIZE + ", " + ORDERED_QUANTITY + ", "
              + INVENTORY_PRICE + ", " + C_STREET_ADDRESS + ", " + C_CITY + ", " + C_STATE +
              ", " + C_ZIP + ", " + C_PHONE +
              " FROM " + TABLE_INVOICE + " as inv INNER JOIN " + TABLE_CUSTOMER + " as cus " +
              "ON inv." + C_CUSTOMER_ID + " = " + "cus." + C_CUSTOMER_ID +
              " INNER JOIN " + TABLE_ORDERED + " as ord ON inv." + INVOICE_ID + " = " + "ord." + ORDERED_ID +
              " INNER JOIN " + TABLE_INVENTORY + " as inventory " +
              "ON ord." + INVENTORY_ID + " = " + " inventory." + INVENTORY_ID, null);
      if(res != null)
          res.moveToFirst();
      return res;
  }
  </code></pre>