I'm developing an Android application:
I have this Enum:
public enum Gender
{
male (0, MyApplication.getAppContext().getString(R.string.male)),
female (1, MyApplication.getAppContext().getString(R.string.female));
private final int gender;
private final String description;
Gender(int aGender, String aDescription)
{
gender = aGender;
description = aDescription;
}
public int getValue() { return gender; }
@Override
public String toString() { return description; }
/**
* Returns all values in this Enum sorted alphabetically by
* description.
* @return All values sorted.
*/
public static Gender[] getSortedVaules()
{
Gender[] sorted = values();
Arrays.sort(sorted, EnumByNameComparator.INSTANCE);
return sorted;
}
}
Imagine I have int gender = 1. In this case, 1 is the value for Gender.female.
I want to use gender variable to get the index of Gender.female enum in the array returned by Gender.getSortedValues().
I think I have to use gender variable to get an its Gender representation, in other words, to get an enum variable with Gender.female as value. And then, use that enum variable to search on Gender.getSortedValues(). But I don't know how to get an enum using its value.
How can I do that?