FilipeOS FilipeOS - 3 months ago 19
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("")){
tvParkAddress.setText(parkAdd);
}


Thank you.

Answer

The R.string.xxx is actually a constant and the value can't be appended to, the resource will never be found. You can search for R.java 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;
      break;
   case 12346: 
      parkAddr = R.string.stg_ParkAddress_12346;
      break;
}
Comments