1

I was working in an App Android using Java. I have to translate some error messages defined in a interface. However the interface don't accept atribuition of R.string resources to a constant like that

public static final String TITLE_AUTO_TEST_NOT_CONNECTED =  "Connection Failed !!!";

Then I will define all contansts directely in strings.xml resources...

However, in this interface I have a HashMap code as follows:

public static Map<Integer,String> errorCodeMap= new HashMap<Integer,String>(){
    {
        put(ERROR_CODE_GENERIC,  R.string.GENERIC_ERROR);
        put(ERROR_CODE_USER_CANCEL, "User action happened !!! ");
        put(ERROR_CODE_COMM_GENERIC, "Communication error happened !!! ");
        put(ERROR_CODE_EXPIRED_TIMER, "Execution time expired !!! ");
        put(ERROR_CODE_NETWORK_GENERIC, "Network error happened !!! "); }
};

In the first put, Java don't accept R.string because the return is int. Could anyone help me with a solution to converto to string... in this interface if possible?

Thanks in advance!

  • 2
    You will need to call `context.getResources().getString(R.string.GENERIC_ERROR)`. The trickiest part for you will then be to get a reference to the context. The best idea IMHO is to store the application context in the class you use (if not, then do it) to override the Application – NSimon Jun 03 '16 at 14:39
  • 1
    To add to @Nicolas Simon's answer, check out this thread for getting a context staticly http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android – liminal Jun 03 '16 at 14:44
  • @liminal That's exactly what I had in mind, thank you – NSimon Jun 03 '16 at 14:46
  • Oh guys! Many thanks. It works!!! – Dirceu Henrique Jun 03 '16 at 15:10

0 Answers0