Java – my android projects are in enum and string Duplicate declaration between XML

I have a string array for spinner and so on

<string-array name="Animal">
    <item >Cat</item>
    <item >Dog</item>
    <item >Rabbit</item>
    <item >Bird</item>
</string-array>

I have a container class to contain values entered by the user or selected from spinner Member variables are mainly primitives or enumerations

public class Container {
    protected int index;
    protected int age;
    protected String name;
    protected Animal pet;
}

This is how to define an enum class (note that this is a simpler version. In my actual code, there are more methods or variables, and each Enum instance is capitalized, such as cat)

public enum Animal {
    Cat,Dog,Rabbit,Bird,}

As you can see, I actually repeat the same thing in enum and XML According to dry, this is not very good Which should I delete and what should I do?

At this point, I prefer to keep enum because it is more versatile than XML and equally maintainable The only possible problem I may be concerned about is its performance They may require more memory and some extra coding

Solution

If you want to avoid repetition, you should keep enum and fill the spinner with for (animal: animal. Values()) {...}

You can use enumeration names as keys to retrieve localized tags or something like that

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>