开发者

Android/Java: using a string for a variable

I am trying to read in values from a string array in an xml file that contains many arrays. Currently I am using this to import the array.

String[] NEWARRAYNAME = getResources().getStringArray(R.array.ARRAYNAME);

what I would like to do is use a random开发者_高级运维 number to get a random array from my file. I was thinking of naming all my arrays in the xml document : m1, m2, m3 ect since I couldn't just use a number(I'm trying to emulate a DB tag). This is what I am doing at the moment to generate a string that incorporates the random number.

int i = random.nextInt(3)+ 1;
String ID = "m" + Integer.toString(i);

So how can I use the string ID to get the array from my xml file. using GetResources().getStringArray(R.array.ID) doesn't work.


You can use getIdentifier to get the identifier for the name you'd otherwise get from R. Note that it's much slower than using the identifier straight from R. You could also put the IDs from R into an array and choose one randomly from that.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜