Reputation: 259
I am trying to implement a user search through my database with the use of spinners.
I have fleets and vehicles(fleets contain vehicles). I have a list of fleets as one spinner and a list of vehicles as the other.
By default I want the fleets to be set to "All" and the vehicle one to show all the vehicles(This is currently the case), however when the fleet input is changed to a particular one, say fleet1, the vehicle spinner should update accordingly[this will be implemented via SQLite database search but I don't think the issue is here].
How do I make a listener for when fleet spinner data is changed?
vehicleSpinner = (Spinner) findViewById(R.id.vehicleSpinner);
String selected = (String)fleetSpinner.getSelectedItem();
ArrayAdapter<String> adapter5 = null;
if(selected == "All"){
//show all vehicles
adapter5 = new ArrayAdapter<String>(this, R.layout.sherlock_spinner_item, vehicleArrayListString);
}else{
String vehiclesInFleetQuery = "SELECT * FROM " + Database.TABLE_VEHICLE + " WHERE " + Database.COLUMN_FLEET + "='" + selected +"'";
Log.i(TAG,"QUERY: "+ vehiclesInFleetQuery);
Cursor cursor = Database.listOfVehiclesDesired(query);
if(cursor.moveToFirst()){
do {
String addToList = cursor.getString(cursor.getColumnIndex(Database.COLUMN_VEHICLE_ID));
vehicleArrayFleet.add(addToList);
} while (cursor.moveToNext());
}else{//error on fleet search, no vehicles in fleet
vehicleArrayFleet = vehicleArrayListString;
builderContinue.setMessage("Selected Fleet(" + selected + ") had zero associated vehicles").setTitle("Error").show();
}
if (cursor != null && !cursor.isClosed()) {
System.out.println("Closed");
cursor.close();
}
adapter5 = new ArrayAdapter<String>(this, R.layout.sherlock_spinner_item, vehicleArrayFleet);
}
adapter5.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
vehicleSpinner.setAdapter(adapter5);
Upvotes: 0
Views: 752
Reputation: 306
You need to set an OnItemSelectedListener for the fleet spinner. You can find an example at http://start-jandroid.blogspot.com/2011/01/android-spinner-example.html. From the listener in the fleet spinner, you can set the selected item of the vehicle spinner.
Upvotes: 1