How do I write to strings.xml with data from a database in Android?
I am trying to make my Android application more dynamic. All the strings are in the strings.xml file and retrieved with R.string. Now I would like to fill strings.xml with data from a database. I know how to开发者_运维问答 fetch data and use it, but I don't understand how I can write to the strings.xml. I read that SharedPreferences is one way of retrieving key-value pairs of data, but not sure if that is the best way to go.
Thanks for any suggestions!
You can't. Resources are precompiled. In strings.xml are stored constant values. Dynamic solution should be based on database.
You can't write to strings.xml. In is included in your apk file during build time and is not modifiable.
Strings.xml is processed at compile-time, you can't modify it dynamically.
You should try to make two folders of string and from database according to users preference stored in database you should load the strings from that folder.
public static String getResourceString(String name, Context context)
{
int nameResourceID = context.getResources().getIdentifier(name, "string", context.getApplicationInfo().packageName);
if (nameResourceID == 0) {
throw new IllegalArgumentException("No resource string found with name " + name);
} else {
return context.getString(nameResourceID);
}
}
精彩评论