Try to select an option in a spinner from the db

Go To StackoverFlow.com

0

I am populating several fields based on data from the my db. I am having trouble figuring out how to use the data from the db to select an item that is in the spinner. The Spinner has four options and when entering data into the db I choose one. Now when I call the db I want to have that option show up as the one chosen in the spinner. Basically I am trying to get the option in the db to be the one showing in the spinner. The commented out line are what I have tried, but do not work. Any ideas?

 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();
    }

update: Found the answer at How to set selected item of Spinner by value, not by position?. The second answer with 48 votes

2012-04-03 20:02
by Aaron


1

I didn't exactly understand your question, however

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

must not even compile. You probably wish to do

truckNumber.setAdapter(aa);
2012-04-03 20:13
by pouzzler
I am trying to get the option the db to be the one selected in the spinne - Aaron 2012-04-03 20:20
Ads