拥有多个字符串的方法

时间:2013-12-20 15:40:30

标签: android sql cursor spinner

我知道这不正确,因为它不起作用,但我不确定为什么这不起作用。

我通过查询从数据库填充了多个微调器。

vTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
                pos, long id) {
                 if(pos!=0){

                    Cursor vTypeChose = (Cursor)(vTypeSpinner.getSelectedItem());
                    if (vTypeChose != null) {
                        String typePicked = vTypeChose.getString(
                        vTypeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));
                        vMake = (Cursor) DataBaseHelper.getPowersportsMake(typePicked);
                        scaMake.changeCursor(vMake);
                        Log.e("SpinnerTest", "Type Selected: " + vType.getString(vType.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE)));


                    }
                 }
            }

    vMakeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
                pos, long id) {
                 if(pos!=0){

                Cursor vMakeChose = (Cursor)(vMakeSpinner.getSelectedItem());
                if (vMakeChose != null) {
                    makePicked = vMakeChose.getString(
                    vMakeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_MAKE));
                    vYear = (Cursor) DataBaseHelper.getPowersportsYear(typePicked, makePicked);
                    scaYear.changeCursor(vYear);
                    Log.e("SpinnerTest", "Make Selected: " + vMake.getString(vMake.getColumnIndex(DataBaseHelper.POWERSPORTS_MAKE)));


                }
             }
        }

选择vMakeSpinner项后,它会启动vYearSpinner代码

这是我的年度微调查询

public static Cursor getPowersportsYear(String typePicked, String makePicked){
return myDataBase.query(POWERSPORTS_TABLE, 
        new String [] {POWERSPORTS_ID, POWERSPORTS_YEARS}, 
        POWERSPORTS_TYPE+"='"+typePicked+"'" +" AND " + POWERSPORTS_MAKE+"='"+makePicked+"'", 
        null, 
        POWERSPORTS_YEARS, 
        null, 
        null);
}

如果我在DataBaseHelper方法中只有一个字符串值,它可以正常工作但只有一个参数。

更新了一些代码:

public class PowersportsEquivalent extends Activity {

DataBaseHelper myDbHelper;
String typePicked;
String makePicked;
String yearPicked;
SimpleCursorAdapter scaMake;
SimpleCursorAdapter scaYear;

Cursor vMake;
Cursor vYear;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_powersports_equivalent);

2 个答案:

答案 0 :(得分:0)

我注意到我需要在游标中再次获取字符串值。所以我补充道:

typePicked = vType.getString(
                vType.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));

在我的制作光标中,我的旋转器现在正常工作。

答案 1 :(得分:0)

你已经用过了

String typePicked = vTypeChose.getString(
                        vTypeChose.getColumnIndex(DataBaseHelper.POWERSPORTS_TYPE));
vTypeSpinner.setOnItemSelectedListener()中的

,它使typePicked成为局部变量。我认为你没有初始化全局typePicked 。在上面的代码中删除字符串,然后您的代码应该正常工作。