Monday 15 July 2013

java - Sort Enum.values() alphabetically and the add them to a Spinner ArrayAdapter -


I am developing an Android application that works with a web service.

I want to sort alphabetically Enum.values ​​() from its description.

I have this Enum :

  public enum sketch {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 finals x stitches personal end string string string; Statement (int ester, string eststrststring) {height = anStature; StatureString = aStatureString; } Public int getValue () {return height; } @ Override public string toasting () {string of return height; }}   

It may not be the best choice to use Enum , but I am very new to Java development.

Do you know how can I sort alphabetically by using my what statureString ?

I want to do this because I want to use these Enum. Values ​​() ArrayAdapter on a :

  mSpinStature.setAdapter (New ArrayAdapter & lt; height & gt; (mActivity, android.R.layout. Simple_dropdown_item_1line, height values ​​()));   

and I want to get the height selected items here:

  Private OnItemSelectedListener listener = new OnItemSelectedListener () {@Override Public Zero onItemSelected (AdapterView & LT;? & Gt; Parents, see, integer status, long support) {if (parent.equals (mSpinStature)) {item item = (height) parent.getAdapter () GetItem (POS). Logs. V ("TAG", item.toString () + item.getValue ()); }} @OtherRide Nothing selected at public zero (adapter view & lt;? & Gt; arg0) {}}; About    

How to add a method to enum

 < Code> height size {tall, average, small; Public static stance [] Get derived wired () {stature [] structure = value (); Arrays.sort (statures, EnumByNameComparator.INSTANCE); Return Stretcher; } Private Static Class EnumByNameComparator implements Comparator & lt; Enum & lt ;? & Gt; & Gt; {Public Fixed Final Comparator & lt; Enum & lt ;? & Gt; & Gt; INSTANCE = new EnumByNameComparator (); Public Integer Comparison (Enum & LT ;? & gt; Enum1, Enum & lt;? & Gt; Enum2). {Return enum1.name () compareTo (enum2.name ()); }}} Public Class StertTest {@Test Public void getSortedValues ​​() {Stature [] normalValues ​​= Stature.values ​​(); Assurance Asset Aqualls (Stature.tall, General Value [0]); Assert.assertEquals (Stature.Average, General Value [1]); Assert.assertEquals (Stature.small, General Value [2]); Size [] Sorted Voices = Height Gateseted Woules (); Assert.assertEquals (Stature.Average, Sortwalls [0]); Assert.assertEquals (Stature.small, sorted voices [1]); Assert.assertEquals (Stature.tall, sorted voices [2]); }}    

No comments:

Post a Comment