Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder alphabetically some items (not all) on an Enum
    primarykey
    data
    text
    <p>I'm developing an Android app and I have this enum that is going to be shown in a Spinner ordered alphabetically using <code>statureString</code>.</p> <pre><code>public enum Stature { noSelected (-1, MSDApplication.getAppContext().getString(R.string.no_selected)), tall (0, MSDApplication.getAppContext().getString(R.string.stature_tall)), average(1, MSDApplication.getAppContext().getString(R.string.stature_average)), small(2, MSDApplication.getAppContext().getString(R.string.stature_small)); private final int stature; private final String statureString; Stature(int anStature, String anStatureString) { stature = anStature; statureString = anStatureString; } public int getValue() { return stature; } @Override public String toString() { return statureString; } public static Stature[] getSortedVaules() { Stature[] statures = values(); Arrays.sort(statures, EnumByToStringComparator.INSTANCE); return statures; } } private static class EnumByToStringComparator implements Comparator&lt;Enum&lt;?&gt;&gt; { public static final Comparator&lt;Enum&lt;?&gt;&gt; INSTANCE = new EnumByToStringComparator(); public int compare(Enum&lt;?&gt; enum1, Enum&lt;?&gt; enum2) { return enum1.toString().compareTo(enum2.toString()); } } </code></pre> <p>And here is how I add set this enum as an ArrayAdapter for a <code>Spinner</code>:</p> <pre><code>private void setSpinStatureData() { mSpinStature.setAdapter(new ArrayAdapter&lt;Stature&gt;(mActivity, android.R.layout.simple_dropdown_item_1line, Stature.getSortedVaules())); } </code></pre> <p>As you can see the first item on the Enum is <code>noSelected</code> and I need that this must always the first item on the Spinner, and the next will be ordered alphabetically.</p> <p>I use <code>statureString</code> because this var is going to be localized.</p> <p><strong>How can I do to make noSelected to be the first item?</strong></p>
    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.
 

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