0

I'm writing an android application with java code. The app can support english and italian. Inside the app, there is a spinner that take its values from an enumerate class, the following:

public enum ElementTypesEnum {
    MEET("Meet"),
    CEREAL("Cereal"),
    FISH("Fish"),
    OTHER("Other");

    private String elementType;

    ElementTypesEnum(String elementType) {
        this.elementType = elementType;
    }

    public String getElementType() {
        return elementType;
    }
}

I want to initialize the values of the enumerate with the values contained in my local string resource file (R.string.value_1). In this class I don't have an instance of the resource file, since I don't have an instance of Context. How can I do this? Thank you in advance, Marco

marco94
  • 157
  • 1
  • 7

1 Answers1

0

Use the resource ID then fetch the strings with your spinner's Context when you populate it.

public enum ElementTypesEnum {
    MEAT(R.string.meat),
    CEREAL(R.string.cereal),
    FISH(R.string.fish),
    OTHER(R.string.other);

    @StringRes
    private int elementType;

    ElementTypesEnum(@StringRes int elementType) {
        this.elementType = elementType;
    }

    public int getElementType() {
        return elementType;
    }
}
92AlanC
  • 1,327
  • 2
  • 14
  • 33