Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's actually a fairly straight-forward way to do this, and to do it in a way that doesn't depend on a particular <code>enum</code>. I ran into this problem when wanting to use <code>enum</code>s to populate different parts of the UI.</p> <p>Here is a little bit of sample code to show you how I did it:</p> <pre><code>public int enumPosition(Enum&lt;?&gt; lookingFor, Enum&lt;?&gt;[] lookingIn) { for (int i = 0; i &lt; lookingIn.length; i++) { if (lookingIn[i].getValue().equals(lookingFor.getValue())) { //Found Answer } } } </code></pre> <p>Of course, depending on what you need there are good ways to setup your <code>enum</code> to allow for better access and searching, here's what I mean:</p> <pre><code>public enum ExampleEnum { firstValue("#1", 1), secondValue("#2", 2), thirdValue("#3", 3); private final String id; private final int somethingUseful; ExampleEnum(String id, int usefulValue) { this.id = id; this.somethingUseful = usefulValue; } public String getValue() { return id; } public int getSomethingUseful () { return somethingUseful; } public static ExampleEnumfromString(String text) { if (text != null) { for (ExampleEnumt : ExampleEnum.values()) { if (text.equalsIgnoreCase(t.id)) { return t; } } } //Usually best to throw an exception here return ExampleEnum.firstValue; } //Useful for passing to adapters public static String[] getValues() { String[] vals = new String[ExampleEnum.values().length]; int i = 0; for (ExampleEnumt : ExampleEnum.values()) { vals[i] = t.getValue(); i++; } return vals; } @Override public String toString() { return getValue(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload