Can't set selection value of Spinner with object
I have spinner
:
List<Vehicle> vehicles = dataSource.getAllVehicles();
Spinner spVehicle = (Spinner) dialog.findViewById(R.id.spVehicle);
SpinnerVehicleAdapter sAdapter = new SpinnerVehicleAdapter(getContext(), R.layout.spinner_textview, vehicles);
spVehicle.setAdapter(sAdapter);
I want to pre-select the dropdown value of the spinner so that I think I should pre-select it using the appropriate Vehicle
object , right?
Keep in mind that this is inside ArrayAdapter<Oil>
where I have access to this:
Oil oilChange;
oilChange.getVehicleId();
So, with this information, I'm trying to set it like this:
Vehicle v = new Vehicle(getContext(), oilChange.getVehicleId());
int spinnerPostion = sAdapter.getPosition(v);
spVehicle.setSelection(spinnerPostion);
Here are additional methods: (I believe I confirmed below code is not a problem.)
public Vehicle(Context mContext, int vehicleId) {
GasDataSource datasource = new GasDataSource(mContext);
datasource.open();
Vehicle vehicle = datasource.createVehicle(vehicleId);
this.vehicleName = vehicle.getVehicleName();
this.vehicleYear = vehicle.getVehicleYear();
this.vehicleModel = vehicle.getVehicleModel();
this.vehicleMake = vehicle.getVehicleMake();
this.oilDistance = vehicle.getOilDistance();
this.drivenDistance = vehicle.getDrivenDistance();
datasource.close();
}
And then:
public Vehicle createVehicle(long id) {
if (!database.isOpen()) {
open();
}
Vehicle vehicle = new Vehicle();
Cursor cursor = database.query(MySQLiteHelper.TABLE_VEHICLE, allVehColumns, MySQLiteHelper.COLUMN_VEHICLE_ID + " = " + id, null, null, null, null);
while (cursor.moveToNext()) {
vehicle = cursorToVehicle(cursor);
}
// Make sure to close the cursor
cursor.close();
return vehicle;
}
The dropdown now spinner
uses the property vehicle.vehicleName
;
And I have:
public String toString() {return getVehicleName(); }
... in my car class.
Also, I have no glitch, I just cannot set the selection based on the provided vehicle object.
With logging, I have confirmed that I am creating the correct object based on the list string. So mine SQL
is good. But I am returning -1 in spinnerPosition
.
Could this work in my method? Or do I need to match on String
not Object
s?
EDIT: THIS IS MY WORK CODE
Vehicle v = dataSource.findVehicle(oilChange.getVehicleId(), vehicles);
if (v != null) {
int spinnerPostion = sAdapter.getPosition(v);
spVehicle.setSelection(spinnerPostion);
}
public Vehicle findVehicle(long id, List<Vehicle> list) {
for(Vehicle vehicle : list) {
if(vehicle.getId() == id) {
return vehicle;
}
}
return null;
}
source to share
Vehicle v = new Vehicle(getContext(), oilChange.getVehicleId());
int spinnerPostion = sAdapter.getPosition(v);
spVehicle.setSelection(spinnerPostion);
I think the code you are using is the problem. You are using a new Vehicle instance to get the position of the object from the sAdapter. ArrayAdapter.getPosition (T element) will only work if you get an object that is part of the adapter, and in your case, that will be a list from dataSource.getAllVehicles ().
List<Vehicle> vehicles = dataSource.getAllVehicles();
You will always get -1 since the car you are looking for is a newly created one and it is not on this list. Use a vehicle instance that is part of the original list you used. Perhaps by creating a function in your datasource and passing in the vehicle id and returning the vehicle instance you created when you called getAllVehicles.
source to share