20

In my app, I have a Spinner being filled from an enum:

ArrayAdapter<myEnum> enumAdapter = new ArrayAdapter<Stroke> (parentActivity.getApplicationContext(), R.layout.simple_spinner_item, myEnum.values());
enumAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
enumSpinner.setAdapter(strokeAdapter);

This uses an override of the enum's toString() method to get a friendly name for the enum values to display in the Spinner. Currently my enum has strings hardcoded for the friendly names but I'd like to move these to strings.xml to support localization.

However, toString doesn't have access to a Context so I'm not sure how to resolve the resource ids.

Is there any way of getting localised strings in the toString() method of an enum?

fortyCakes
  • 322
  • 1
  • 3
  • 14
  • 2
    An enum is just shorthand for a class. Create a singleton class which does the work. The toString() method can then access whatever it wants to. – Simon Sep 27 '12 at 15:44
  • Why use the actual string? Most `View.setText()` for example can also take a resourceID. In a similar way, option builders can take arrays of resourceIDs instead of string arrays. – ccpizza Oct 11 '15 at 15:06

4 Answers4

34

If I understand this correctly, the real question here is how to get a Context from your enum, so that you can call Context.getString() to get localized versions of the Strings you need.

One approach, would be to set a static member variable of type Context in your application's onCreate() method, which is described in this answer. The idea here is that every time your application gets created or recreated, you'll hold on to the application context in a variable that's easy to get to.

Then, pass in the resource ID in the constructor of your enum values, and use the Context in your toString() method.

For example:

public enum Example {
    HELLO(R.string.hello),
    WORLD(R.string.world);

    private int mResourceId;

    private Example(int id) {
        mResourceId = id;
    }

    @Override
    public String toString() {
        return App.getContext().getString(mResourceId);
    }
}
Community
  • 1
  • 1
wsanville
  • 37,158
  • 8
  • 76
  • 101
7
@Override
    public String toString()
    {
        return App.getContext().getString(id);
    }

It's important that your enum class doesn't have any ties to your activity because if you want to use it in another application then you won't be able to if you are referencing a static context.

So a better way would be to return the resource id of the string back to the activity and then let the activity grab the string using the id from there.

So from your enum class you would have a method looking something similar to this:

  public  int getResourceId()
  {
    return resourceId;
  }

Then in your activity I would build up a list containing an arraylist:

final List<String> enumList = new ArrayList<String>();
for ( final MyEnum status : MyEnum.values() )
{
  enumList.add( getString( status.getResourceId() ) );
}

Then you can use enumList with your ArrayAdapter. Bingo :)

So now you have no ties to the enum class, and so if you are building another app that needs to use the same enum class you can easily do so.

Sambuxc
  • 425
  • 2
  • 11
  • 26
4

Use static Application is always a bad practice, because not only it breaks Instant Run, but also this is against the decoupling principle thus makes modularization difficult to implement. Not to mention Android actually supports multiple Applications in a single process.

For this reason, I'd suggest define an inner class for the enum as your adapter entries.

enum Example {
    A(R.string.label_a),
    B(R.string.label_b);

    Example(@StringRes int label) { mLabel = label; }
    private @StringRes int mLabel;

    class Entry {
        private final Context mContext;
        Entry(final Context context) { mContext = context; }
        @Override public String toString() { return mContext.getString(mLabel); }
    }
}

Then, build an array of Example.Entry for the adapter.

Arrays.stream(Example.values()).map(item -> item.new Entry(context)).toArray(Example.Entry[]::new)
Oasis Feng
  • 7,490
  • 3
  • 31
  • 44
1

I'm not 100% sure I understand what you are asking.

Is there any way of getting localised strings in the toString() method of an enum?

You can certainly @Override the toString() method inside of your myEnum to change how it is displayed:

public enum myEnum {
    ONE("1"),
    TWO("2");
    private String pretty;
    private myEnum(String pretty) {
        this.pretty = pretty;
    }
    @Override
    public String toString() {
        // you can localise this string somehow here
        return pretty;
    }
}
Gray
  • 115,027
  • 24
  • 293
  • 354
  • 1
    I'm looking for a way to use the Android R.string.foo ids to lookup a translated version of the string inside the toString method, where you don't normally have access to an application context. – fortyCakes Sep 28 '12 at 09:18