Reputation: 2909
I would like to persist the Enum not as String and not as the ordinal but rather as the given number in the constuctor:
public enum EAdUnitType {
NOTIFICATION(1),
BANNER(2);
private int mId;
public int getId() {
return mId;
}
}
I would like to persist the getId().
Upvotes: 2
Views: 102
Reputation: 120
Use can add to the enum from id
public enum EAdUnitType {
NOTIFICATION(1),
BANNER(2);
private int mId;
public int getId() {
return mId;
}
public static EAdUnitType fromId(int id) {
EAdUnitType [] types = EAdUnitType .values();
for (EAdUnitType eType : types) {
if (eType.mId == id) {
return eType;
}
}
return null;
}
}
and save the numeric id into to the database
public class DbEntity {
private int mEAdUnitTypeId;
.....
public void setEAdUnitType(EAdUnitType type) {
mEAdUnitTypeId = type.getId();
}
public EAdUnitType getEAdUnitType() {
return EAdUnitTypeId.fromId(mEAdUnitTypeId);
}
.........
}
Upvotes: 2