Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put ENUM values to android spinner?

For example: I have an enum with days.

How do I put its values into spinner ?

like image 827
saikek Avatar asked Mar 29 '11 08:03

saikek


2 Answers

Similar to another answer, but you can use an ArrayAdapter to populate based on an Enum class. I would recommend overriding toString in the Enum class to make the values populated in the spinner more user friendly. In the activity:

Spinner mySpinner = (Spinner) findViewById(R.id.mySpinnerId);

mySpinner.setAdapter(new ArrayAdapter<MyEnum>(this, android.R.layout.simple_spinner_item, MyEnum.values()));

Your enum class:

public enum MyEnum{
    ENUM1("Enum 1"),
    ENUM2("Enum 2");

    private String friendlyName;

    private MyEnum(String friendlyName){
        this.friendlyName = friendlyName;
    }

    @Override public String toString(){
        return friendlyName;
    }
}
like image 100
Adam Avatar answered Nov 13 '22 01:11

Adam


Some kind of walkthrough is using:

Spinner mySpinner = (Spinner) findViewById(R.id.cmbClothType);
mySpinner.setAdapter(new ArrayAdapter<MyType>(this, android.R.layout.simple_list_item, MyType.values()));
like image 21
saikek Avatar answered Nov 13 '22 01:11

saikek