How can access particular String from Resource file with using Resource.getString(id) in Android
I want to know that I have the string.xml Resource file which contains status messages. Now I want to process them according their id. I have written the following code but it fails on Message = res.getString(Msgid);
and logs Resource not found Exception
.
Can any one help me?
public void FileStatusMsg(int Msgid)
{
String Message = null;
Resources res = this.getResources();
Message = res.getString(Msg开发者_运维问答id);
//Display Status Messages..
AlertDialog.Builder builder = new AlertDialog.Builder(OfficeLinQ.this);
builder.setMessage(Message).setCancelable(false).setPositiveButton(
"OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
Suppose you have a value
<string name="msg">Message</string>
inside strings.xml
.
To access this value, in your activity give:
this.getText(R.string.msg).toString()
you can access it by using this method :
YourActivity.this.getResources().getString(R.string.myString);
精彩评论