Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access resource strings from enum's ToString in Android?

Tags:

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?

like image 991
fortyCakes Avatar asked Sep 27 '12 15:09

fortyCakes


2 Answers

@Override public String toString() { return App.getContext().getString(id); }

It's important that your enum class doesnt have any ties to your activity because if you want to use in another application then you won't be able to if your 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 their.

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 your building another app that needs to use the same enum class you quality easily do so.

like image 27
Sam Bruton Avatar answered Oct 05 '22 23:10

Sam Bruton


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);
    }
}
like image 121
wsanville Avatar answered Oct 05 '22 22:10

wsanville