0

In my Android application, I want to set the ArrayList<User> to the spinner. I don't know how to set the above list to spinner. Please tell me how to do.

Below is the User bean class.

 class User {
   String userId;
   String userName;
   String userCountry;

   public String getuserId() {
    return CountryId;
}
public void setuserId(String userId) {
    userId = userId;
}
public String getuserName() {
    return userName;
}
public void setuserName(String userName) {
    userName = userName;
}
public String getuserCountry() {
    return userCountry;
}
public void setuserCountry(String userCountry) {
    userCountry = userCountry;
}
    @Override
public String toString() {
    // TODO Auto-generated method stub
    return userName;
}
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
user2636874
  • 889
  • 4
  • 15
  • 36
  • 1
    you will need to create [Custom Adapter](http://adanware.blogspot.in/2012/03/android-custom-spinner-with-custom.html) – ρяσѕρєя K Feb 25 '14 at 05:37
  • What is wrong you override toString and return userName. Use a `ArrayAdapter` and set the same to spinner and if you don't want just username use a custom adpater – Raghunandan Feb 25 '14 at 05:37
  • http://stackoverflow.com/questions/7955424/creating-a-spinner-from-an-arraylistobject this and http://stackoverflow.com/questions/6562236/android-spinner-databind-using-array-list this might help you.. – Arshad Ali Feb 25 '14 at 05:37
  • Look at this [https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/](https://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-android/) – M D Feb 25 '14 at 05:38

1 Answers1

1
Spinner spinner = (Spinner) findViewById(R.id.your_spinner);
List<User> list = new ArrayList<User>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41