Populating a Spinner with an Enum having a string resource

I have an enum that has a property that maps to the id of a string resource, for example:

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 to fill a counter with an enum, the problem is just using an adapter of my type:

Spinner spinner;
spinner.setAdapter(new ArrayAdapter<MyEnum>(this, android.R.layout.simple_spinner_item, MyEnum.values()));

      

I am not getting a description of the string resource as the adapter implicitly calls toString () which returns the name of the enumeration. I do not know how to do that. No matter what I need the context for to get the string values. Can anyone suggest a better way to achieve what I'm looking for? I just need a point in the right direction. Any advice will be taken into account. Many thanks!

+3


source to share


2 answers


You have to create your own ArrayAdapter. Then override the getView () and getDropDownView methods.



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;
    }
}

      

+8


source


The easiest way to get your enum is provided its view position, or position position gets its position: when you pass in the enum values, they will appear in the order in which they are declared. For example, in OnItemClickListener

:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    MyEnum value = MyEnum.values()[position];
    ...
}

      


Another solution would be to get the text of the view and then parse the enum using Enum.valueOf, but this requires knowing the ID TextView

that the element is displaying and seems tricky compared to the first solution. Something like:



public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    String text = ((TextView) parent.findViewById(R.id.mytextview)).getText();
    MyEnum value = MyEnum.valueOf(text);
}

      

And finally, you can also extract the enum from the adapter and cast it like this:

MyEnum.class.cast(myListView.getAdapter().getItem(position))

      

+2


source







All Articles