java - Getting an enumerated value out of an Android Spinner -
i've written class below let me enumerated value out of android spinner.
there 2 lines in getvalue()
neither of compile.
how should this?
public class enumspinnerlistener<t extends enum> implements adapterview.onitemselectedlistener { private string mvalue = null; public enumspinnerlistener(adapterview<?> adapterview) { adapterview.setonitemselectedlistener(this); } @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { mvalue = adapterview.getitematposition(i).tostring(); } @override public void onnothingselected(adapterview<?> adapterview) { // nothing } public t getvalue() { return enum.valueof(t.class, mvalue); // cannot select type variable return t.valueof(mvalue); // valueof(java.lang.class<t>, string) in enum cannot applied (java.lang.string) } }
due type erasure, t
have no meaning @ runtime, why expression t.class
illegal. workaround reference class<t>
instance:
public class enumspinnerlistener<t extends enum<t>> // note correction here implements adapterview.onitemselectedlistener { private final class<t> type; private string mvalue = null; public enumspinnerlistener(class<t> type, adapterview<?> adapterview) { this.type = type; adapterview.setonitemselectedlistener(this); } public t getvalue() { return enum.valueof(type, mvalue); } }
Comments
Post a Comment