FilipeOS FilipeOS - 9 months ago 90
Java Question

Change Resource getString() programatically

Is there any way to change a R.string programatically? Because it's throwing an error.

Basically I want to do this:

String parkAdd = getString(R.string.stg_ParkAddress_+id);

Because I have hardcoded strings that are changed according ID.

I tried to do this but don't work:

String parkAdd = getString(R.string.stg_ParkAddress_1);
parkAdd = parkAdd.replace("1",id);
if (!parkAdd.equalsIgnoreCase("")){

Thank you.


The is actually a constant and the value can't be appended to, the resource will never be found. You can search for to see the values for your app:

public static final class string {
     public static final int about_open_source_heading=0x7f060013;
     public static final int about_open_source_list=0x7f060014;

If you have hard coded strings that depend on a specific value, maybe you can do something like this:

switch ( id ) {
   case 12345: 
      parkAddr = R.string.stg_ParkAddress_12345;
   case 12346: 
      parkAddr = R.string.stg_ParkAddress_12346;