Hardcoding Int reference as String to create JAR

I have a situation where I am creating JAR and I don't want to reference any of String or Int from /res/values. So I am hardcoding it everywhere

event_info.put("CLIENT_HALT", new EventInfo(R.string.client_halt, R.drawable.error, 0, EV_PRIO_HIGH, 0));

As you see "R.string.client_halt" refers to String "Halt" in res/values/abc.xml

But it is of type Int, hence I cannot do like this

event_info.put("CLIENT_HALT", new EventInfo("Halt", R.drawable.error, 0, EV_PRIO_HIGH, 0));

Below is EventInfo method

public EventInfo(int res_id_arg, int progress_arg, int priority_arg, int flags_arg)
    {
        res_id = res_id_arg;
        //icon_res_id = icon_res_id_arg;
        progress = progress_arg;
        priority = priority_arg;
        flags = flags_arg;
    }

How do I overcome this issue and hardcode int value in my method. I tried below case but it won't work and give me "NumberFormatException"

String halt = "Halt";
int foo = Integer.parseInt(halt);

Thanks

Answers


By passing the id into constructor of your class, you can retrieve the string in the following way.

String stringValue = getResources().getString(R.string.client_halt);

If you are not able to get getResources() directly, pass the context in constructor and do the following

_context.getResources().getString(R.string.client_halt);

Need Your Help

How to save a JSON object to a file using Powershell?

json file powershell

I have converted the following JSON file to powershell representation object.

How to populate Radio Group in Alert Builder Dialog within activity?

android android-activity android-radiogroup

I m new in android. so i can not populate radio group from activity within alert builder dialog, Please help me any one/body.