开发者

How to refer to a random integer in R.raw method in Android?

I have this code

Random x = new Random(44);
int l = x.nextInt();
int path = R.raw.l;

I know it doesn't work that way,but it explains my situation the most. i.e. Providing l==开发者_如何转开发5, i want to use R.raw.5 as the path. however, R.raw seems to understand only straightforward values, not the referred ones. Please help!


You can get int indentifier from your activity with this code:

Random x = new Random(44);
int l = x.nextInt();
int path = getResources().getIdentifier(String.valueOf(l), "raw", getPackageName());

More info in documentation: http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜