开发者

How to access R.string.xxx resources from a method by passing string 'xxx' as parameter to that method?

I have many string elements in my res/values/strings.xml

So, I want on开发者_JAVA技巧e method getString(String abc) for retrieving the strings from strings.xml:

public String getString(String abc){ // abc = address1

    String result;

    result = context.getResources().getString(R.strings.+abc);
}

How to access the string elements in this method based on a String in argument?


public String getString(String abc){ // Ex. abc = "address1"

   int resID = getResources().getIdentifier(abc, "string",  getPackageName()); 

   return context.getResources().getString(resID);
}


 String abc="StringId";
 int resID = getResources().getIdentifier(abc, "string",  getPackageName()); 


try This:

Resources res = this.getResources(); 
 int resID = res.getIdentifier(imagename, "drawable", this.getPackageName());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜