title: Android Studio Select item from SQLite database by spinner selection
body: <p>I am working on a project where I open up a random link in the browser after an imagebutton is pressed. I have created and populated a database table with different links and assigned them to categories. I would like to be able to filter which links are selected based upon a users' selection from a spinner dropdown. Right now, I am only able to select a random recipe from the database. </p>
  
  <p><strong>This is the main activity with all the relevant methods (I have left out the import statements and any other unnecessary code :</strong> </p>
  
  <pre><code>package com.example.randomrecipeapp;
  
  
  public class MainActivity extends AppCompatActivity {
  
  //fields
  ImageButton randomizer;
  Spinner spinner_filter;
  DatabaseHelper db;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      db = new DatabaseHelper(getApplicationContext());
      //connect fields
      randomizer = (ImageButton) findViewById(R.id.logo);
      //spinner things
      spinner_filter = (Spinner) findViewById(R.id.spinner_filter);
      // Create an ArrayAdapter using the string array and a default spinner layout
      ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(this,
              R.array.category_array, android.R.layout.simple_spinner_item);
      // Specify the layout to use when the list of choices appears
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      // Apply the adapter to the spinner
      spinner_filter.setAdapter(adapter);
  
      //initialize methods
      openRandomRecipe();
  
  }
  
  ///////////////////////////////////////////////////////////////////
  //spinner response
  public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                             int pos, long id) {
      // An item was selected. You can retrieve the selected item using
      Object item = parent.getItemAtPosition(pos);
  }
  
  public void onNothingSelected(AdapterView&lt;?&gt; parent) {
      // Another interface callback
  }
  
  public void openRandomRecipe() {
  
      randomizer.setOnClickListener(
              new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      Cursor res = db.getRandomData();
  
                      //if no data exists
                      if (res.getCount() == 0) {
                          // show message
                          showMessage("Error", "Nothing found");
                          return;
                      }
  
                      //add recipe link (columnIndex 1) to buffer sequence
                      StringBuffer buffer = new StringBuffer();
                      while (res.moveToNext()) {
                          buffer.append(res.getString(1));
                      }
                      //convert buffer to st
                      String sr = buffer.toString();
                      //open link in browser
                      startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(sr)));
  
                  }
              }
      );
  }
  }
  </code></pre>
  
  <p><strong>The openRandomRecipe() method calls this other method in my DatabaseHelper class:</strong> </p>
  
  <pre><code> //get one random recipe
  public Cursor getRandomData() {
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor res = db.rawQuery("SELECT * FROM "+TABLE_RECIPES+" ORDER BY RANDOM() LIMIT 1",null);
      return res;
  
  }
  </code></pre>
  
  <p>Where TABLE_RECIPES is the name of the table. What I'm thinking I need to do is create a new method such as this: </p>
  
  <pre><code>public Cursor getRandomDataByCategory(String category) {
  SQLiteDatabase db = this.getWritableDatabase();
      Cursor res = db.rawQuery("SELECT "+category+ " FROM "+TABLE_RECIPES+" ORDER BY RANDOM() LIMIT 1",null);
      return res;
  </code></pre>
  
  <p>And then in the onClickListener in the MainActivity file do</p>
  
  <pre><code>Cursor res = db.getRandomData(category);
  </code></pre>
  
  <p>I'm just not sure how to figure out how to pull in the user selected item from the spinner into the getRandomData method. Is this even a correct way of solving this problem? Thanks for the help. </p>