title: select from database where id = value from spinner
body: <p>I have the following problem:
  I have two spinners, first one with categorie1, second categorie2.</p>
  
  <p>each spinner can load it's data from a database from table cat1 and cat2. But that's not exactly what I want. I want for the second spinner to load data where the cat1_id is equals cat_id from currently selected spinner1.</p>
  
  <p>The tables are like this:</p>
  
  <pre><code>cat1:
  | cat1_id | cat1_name |
  =======================
  | 1       | animal    |
  | 2       | plant     |
  | 3       | ...       |
  
  cat2:
  | cat2_id | cat1_id | cat2_name |
  =================================
  | 1       | 1       | fish      |
  | 2       | 1       | mammal    |
  | 3       | 1       | bird      |
  | 4       | 2       | tree      |
  | 5       | ...     | ...       |
  </code></pre>
  
  <p>How do I make this comparison or how do I know, which item is selected in spinner1.</p>
  
  <p>My code is like this:</p>
  
  <p>DatabaseHandler.class:</p>
  
  <pre><code>    public List&lt;String&gt; getCat(){
      labels = new ArrayList&lt;String&gt;();
      db = this.getReadableDatabase();
      cursor = db.rawQuery(SELECT_QUERY_CATEGORIE, null);  // SELECT_QUERY_CATEGORIE = "SELECT  * FROM  cat1";
  
      if (cursor.moveToFirst()) {
          do {
              labels.add(cursor.getString(1));
          } while (cursor.moveToNext());
      }
      cursor.close();
      db.close();
  
      return labels;
  }
  
  public List&lt;String&gt; getSubcat(){
      labels = new ArrayList&lt;String&gt;();
      db = this.getReadableDatabase();
      //cursor = db.rawQuery(SELECT_QUERY_SUBCATEGORIE, null); // SELECT_QUERY_SUBCATEGORIE = "SELECT  * FROM  cat2";
  
      if (cursor.moveToFirst()) {
          do {
              labels.add(cursor.getString(2));
          } while (cursor.moveToNext());
      }
  
      cursor.close();
      db.close();
      return labels;
  }
  </code></pre>
  
  <p>MainActivity.class:</p>
  
  <pre><code>...
  spCat.setOnItemSelectedListener(this);
  spSubcat.setOnItemSelectedListener(this);
  loadSpinnerData();
  
  private void loadSpinnerData() {
      List&lt;String&gt; lbCategorie = db.getCat();
      ArrayAdapter&lt;String&gt; catAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, lbCategorie);
      catAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spCat.setAdapter(catAdapter);
  
  
      List&lt;String&gt; lbSubcategorie = db.getSubcat();
      ArrayAdapter&lt;String&gt; subcatAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, lbSubcategorie);
      subcatAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      spSubcat.setAdapter(subcatAdapter);
  }
  
      public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position,
          long id) {
  }
  
  @Override
  public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
      // Spinner2.Visibility should be = GONE;
  }
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT:
  Alright. I'm loading my Database now with the following code:</p>
  
  <pre><code>int position = MainActivity.spCat.getSelectedItemPosition()+1;
  cursor = db.rawQuery("SELECT * FROM cat2 WHERE cat_id = ?" + position, null);
  </code></pre>
  
  <p>But my second Spinner is empty, whatever I select from the first Spinner. While debugging I noticed, that loading the database was just called once at the beginning.</p>
  
  <hr>
  
  <p>EDIT 2:</p>
  
  <p>Works now after deleting the "?" in my db.rawQuery. However the second spinner isn't it's content after changing the item in the first spinner. How do I make a refresh of the second spinner after calling the first on?</p>
  
  <p>Thanks.</p>