Issue
I have a SQlite table with two columns _id
and name
. For internationalization purpose I store the name like R.string.today
Now I want to get string resource associated with R.string.today
:
String column = myCursor.getString(myCursor.getColumnIndex(MainDb.NAME));
int resId = getApplicationContext().getResources().getIdentifier(column, "strings", getApplicationContext().getPackageName());
String buttonText;
if (resId != 0)
{
buttonText = getApplicationContext().getString(resId);
} else
{
buttonText = "Resource is null!";
}
resId
is always 0. Variable column
have the right value, I've checked it with debugger. I've tried different variations in resId
:
- "string" instead of "strings" in defType.
- Different variation of getResources like Resources.getSystem()...
- Different variation of getPackageName()
What I'm doing wrong?
Solution
I think it should be string
instead of strings
:
int resId = getApplicationContext().getResources()
.getIdentifier(column, "string", getApplicationContext().getPackageName());
Check as well whether the value of column
dsignates an identifier in your strings.xml
.
This is a nice example.
p.s.: instead of R.string.today
just store today
or extract store
from R.string.today
.
Resources r = getResources();
r.getIdentifier("today", "string", getPackageName()));
r.getIdentifier("R.string.today", "string", getPackageName())); // doesn't work
r.getIdentifier("R.string.today".replaceAll(".*\\.", ""), "string", getPackageName()));
Only first and third work.
Answered By - Trinimon
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.