java - Populating Spinner with Enum that has string resource -


i have enum has property maps string resource id, so:

public enum myenum{    first(1,r.string.first_enum_desc),   second(2,r.string.second_enum_desc);    private int mid;   private int mdescriptionresourceid;    private myenum(id,descriptionresourceid) {       mid = id;       mdescriptionresourceid = descriptionresourceid;   }    public tostring(context){       return context.getstring(mdescriptionresourceid);   } } 

i want populate spinner enum, problem using adapter of type:

spinner spinner; spinner.setadapter(new arrayadapter<myenum>(this, android.r.layout.simple_spinner_item, myenum.values())); 

i don't string resource description adapter implicitly calls tostring(), returns enum name. i'm not sure how proceed here. no matter need context string values. suggest best way achieve looking for? need point in right direction. advice appreciated. much!

you should build own arrayadapter. , override methods getview() , getdropdownview.

public class myadapter extends arrayadapter<myenum> {      public myadapter (context context) {         super(context, 0, myenum.values());     }      @override     public view getview(int position, view convertview, viewgroup parent) {          checkedtextview text= (checkedtextview) convertview;           if (text== null) {              text = (checkedtextview) layoutinflater.from(getcontext()).inflate(android.r.layout.simple_spinner_dropdown_item,  parent, false);          }           text.settext(getitem(position).getdescriptionresourceid());           return text;     }      @override     public view getdropdownview(int position, view convertview, viewgroup parent) {         checkedtextview text = (checkedtextview) convertview;          if (text == null) {             text = (checkedtextview) layoutinflater.from(getcontext()).inflate(android.r.layout.simple_spinner_dropdown_item,  parent, false);         }          text.settext(getitem(position).gettitle());          return text;     } } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -