尝试从db中选择一个微调器中的选项

时间:2012-04-03 20:02:51

标签: android database android-spinner

我根据来自我的数据库的数据填充了几个字段。我无法弄清楚如何使用数据库中的数据来选择微调器中的项目。 Spinner有四个选项,当我将数据输入db时,我选择一个。现在,当我调用db时,我希望将该选项显示为在微调器中选择的选项。基本上我试图让db中的选项成为在微调器中显示的选项。注释掉的是我尝试过的,但不起作用。有什么想法吗?

 truckNumber = (Spinner) findViewById(R.id.truck_number);
    drivable = (RadioButton) findViewById(R.id.drivable);
    notDrivable = (RadioButton) findViewById(R.id.not_drivable);
    truck = (RadioButton) findViewById(R.id.truck);
    trailer = (RadioButton) findViewById(R.id.trailer);
    aa = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, trucks);
    bb = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, trailers);
    newTime = new Time();
    edit= new Inspection();
    long something = edit.returnID();
    updateDB= new InspectionDBAdapter(this);
    if(something>0)
    {
        updateDB.open();    
        Cursor result= updateDB.getOneInspection(something);
        result.moveToFirst();
        int odo=result.getColumnIndex("odometer");
        int firstDriver=result.getColumnIndex("driver");
        int secondDriver=result.getColumnIndex("codriver");
        int driveCondition=result.getColumnIndex("status");
        int vehicleType=result.getColumnIndex("vehicle_type");
        int truckNum=result.getColumnIndex("vehicle_id");
        driver.setText(result.getString(firstDriver));
        odometer.setText(result.getString(odo));
        coDriver.setText(result.getString(secondDriver));
    //  truckNumber.setAdapter(aa.getPosition(result.getString(truckNum).to));
    //  truckNumber.setSelection(result.getString(truckNum).toString());
        if(result.getString(driveCondition).equals("1"))
            drivable.setChecked(true);
        else
            notDrivable.setChecked(true);

        if(result.getString(vehicleType).equals("truck"))
            truck.setChecked(true);
        else
            trailer.setChecked(true);

        result.close();
        updateDB.close();
    }

更新:在How to set selected item of Spinner by value, not by position?找到答案。第二个答案是48票

1 个答案:

答案 0 :(得分:1)

我并不完全理解你的问题,但是

truckNumber.setAdapter(aa.getPosition(result.getString(truckNum).toString));

一定不能编译。你可能希望这样做

truckNumber.setAdapter(aa);