I have a situation that I presume is pretty common. I have a Java enum:
public enum Flavour { CHOCOLATE, STRAWBERRY }
I have a custom attribute defined in attrs.xml:
<attr name="flavour">
<enum name="chocolate" value="0" />
<enum name="strawberry" value="1" />
</attr>
But this feels really fragile. It relies on me manually mapping from the attribute to the enum correctly.
If someone adds "SARDINE" to the end of enum Flavour then it obviously won't be automatically added to the attribute definition. That's fair enough.
But worse, if someone adds "SARDINE" in the middle of the enum, it will break xml layouts that use "strawberry".
How do people overcome this? I've considered using string (and using Flavour.valueOf()
), but I was hoping there might be a cleaner solution out there.