Cómo hacer coincidir int con enum
Estoy recibiendo el valor devuelto en forma de long o int del código nativo en Android, que quiero convertir o emparejar con enum, para fines de procesamiento. Es posible ? ¿Cómo?
46
2 answers
Si tienes control total de valores y enumeraciones, y son secuenciales, puedes usar el valor ordinal de enumeración:
enum Heyo
{
FirstVal, SecondVal
}
...later
int systemVal = [whatever];
Heyo enumVal = Heyo.values()[systemVal];
int againSystemVal = enumVal.ordinal();
59
Author: Kevin Galligan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-03 15:10:10
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-11-03 15:10:10
Puede configurar su enum para que tenga el long o int incorporado en él.
Por ejemplo: Crear este archivo ePasswordType.java
public enum ePasswordType {
TEXT(0),
NUMBER(1);
private int _value;
ePasswordType(int Value) {
this._value = Value;
}
public int getValue() {
return _value;
}
public static ePasswordType fromInt(int i) {
for (ePasswordType b : ePasswordType .values()) {
if (b.getValue() == i) { return b; }
}
return null;
}
}
Luego puede acceder a los valores asignados de la siguiente manera:
ePasswordType var = ePasswordType.NUMBER;
int ValueOfEnum = var.getValue();
Para obtener la enumeración cuando solo conoce la int, use esto:
ePasswordType t = ePasswordType.fromInt(0);
Los enums en java son muy poderosos ya que cada valor puede ser su propia clase.
54
Author: Kuffs,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-16 08:47:16
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-16 08:47:16