Basem S H Saabneh Basem S H Saabneh - 5 months ago 13
Android Question

Android , how to get xml resouce value from resouce id using java

In res/ folder I have multilanguage XML files, one for Arabic and the other for English:

<string name="ram"/>ramallah<string>
<string name="nablus"/>Nablus<string>
<string name="jenin"/>Jenin<string>

<string name="ram"/>رام الله<string>
<string name="nablus"/>نابلس<string>
<string name="jenin"/>جنين<string>


I define names like:

int[] names= {R.string.ram, R.string.nablus, R.string.jenin}


I store these values in a database. When I select data I have values like 2131099699, 2131099623, and 21310991231. How can I map these numbers to the names in XML? And how can I get the resource value like ramallah, Nablus, and Jenin from these integer values?

Answer

Instead of saving resource ids in that way, you can save to database the resource name:

String[] names = {"ram", "nablus", "jenin"};

Then you can access the resource value in this way:

int stringId = context.getResources().getIdentifier(nameStored, "string", context.getPackageName());
String stringValue = "default string";
if (stringId != 0) {
   stringValue = context.getResources().getString(stringId);
}
Comments