2013-12-16 41 views
0

從一個答案到過去的罰款問題,我越來越。 我想要將我的微調器中的選定項從我的SQLite數據庫填充爲一個字符串,該字符串用作其他查詢的變量。Android微調選框項目作爲查詢字符串

//POWERSPORTS TYPE Cursor 
    vType = (Cursor) DataBaseHelper.getPowersportsType(); 
    this.startManagingCursor(vType); 

     SimpleCursorAdapter scaType = new SimpleCursorAdapter(this, 
       android.R.layout.simple_spinner_item, 
       vType, 
       new String [] {DataBaseHelper.POWERSPORTS_TYPE}, 
       new int[] {android.R.id.text1}); 

     scaType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     vTypeSpinner = (Spinner) findViewById(R.id.typeSpinner); 
     vTypeSpinner.setAdapter(scaType); 

     vTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){ 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       // TODO Auto-generated method stub 
       String typeSelection = vTypeSpinner.getSelectedItem().toString(); 
      } 
     }); 

    //POWERSPORTS MAKE Cursor 
    vMake = (Cursor) DataBaseHelper.getPowersportsMake(typeSelection); 
    this.startManagingCursor(vMake); 

     SimpleCursorAdapter scaMake = new SimpleCursorAdapter(this, 
       android.R.layout.simple_spinner_item, 
       vMake, 
       new String [] {DataBaseHelper.POWERSPORTS_MAKE}, 
       new int[]{android.R.id.text1}); 

     scaMake.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     vMakeSpinner = (Spinner) findViewById(R.id.makeSpinner); 
     vMakeSpinner.setAdapter(scaMake); 

此外,這裏是我的查詢第二個微調。

public static Cursor getPowersportsMake(String typeSelection){ 
return myDataBase.query(POWERSPORTS_TABLE, 
    new String [] {POWERSPORTS_ID, POWERSPORTS_MAKE}, 
    POWERSPORTS_TYPE+"='"+typeSelection+"'", 
    null, 
    null, 
    null, 
    null); 
} 

我知道我做錯了什麼,因爲我越來越typeSelection中「不能被解析爲一個變量」我

vMake = (Cursor) DatabaseHelper.getpowersportsMake(typeSelection); 

如何使用從我onItemSelected創建的字符串?

回答

0

聲明typeSelection作爲全局變量。目前它在setOnItemSelectedListener之內,因此不可見。

+0

這將我第一次聽到一個全局變量。我將如何做到這一點? – LukeG224

+0

你在你的方法之外聲明它。 – Apoorv

+0

不太確定如何完全做到這一點,但我會谷歌和接受你的答案。謝謝您的幫助。 – LukeG224