I'm populating my spinner with user object in order to work later with the user ID but the display of the user lists shows the address of the object I guess.
So my question is how to display only one attribute of the object, in the case of user name, but still populate the spinner with the whole object
User user1 = new User("user1",24);
User user2 = new User("user2",26);
// Creating adapter for spinner
List<User> users = new ArrayList<User>();
users.add(user1);
users.add(user2);
ArrayAdapter<User> dataAdapter = new ArrayAdapter<User>(this,
android.R.layout.simple_spinner_item, users);
// Drop down layout style - list view
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner _EmpSpinner = null;
_EmpSpinner = (Spinner) findViewById(R.id.EmployeesSpinner);
// attaching data adapter to spinner
_EmpSpinner.setAdapter(dataAdapter);
Let the Spinner's first value be something like "-please select-". when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.
You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .
Try overriding toString()
method in the User
class:
@Override
public String toString() {
return this.name;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With