Android Spinner选择的项目作为查询的字符串

时间:2013-12-16 13:19:50

标签: android sqlite cursor android-spinner simplecursoradapter

从回答过去的罚款问题来看,我已经走到了这一步。 我试图让我的微调器中的选定项目从我的SQLite数据库填充为一个String,用作我的其他查询的变量。

//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创建的String?

1 个答案:

答案 0 :(得分:0)

typeSelection声明为全局变量。目前它在setOnItemSelectedListener之内,因此不可见。