在我values.xml文件中,我有一个数组,像这样;
<字符串数组名=动物阵列>
<项目>牛< /项目>
<项目>猪< /项目>
<项目>鸟< /项目>
<项目>羊< /项目>
< /字符串数组>
在我的应用程序,我想其中一个值是随机的,但我不能确定如何做到这一点。我已经看过
Help在得到字符串数组从arrays.xml文件
这
Retreiving从ArrayList的一个项目,在随机
但我怎么检索我的名单是在values.xml文件中定义一个随机的项目?
解决方案 String []数组= context.getResources()getStringArray(R.array.animals_array)。
字符串randomStr =数组[新的随机()nextInt(array.length)];
希望这有助于。
In my values.xml file, I have an array, like this;
<string-array name="animals-array">
<item>Cow</item>
<item>Pig</item>
<item>Bird</item>
<item>Sheep</item>
</string-array>
In my app, I want to get one of these values at random, but I am unsure how to do this. I have looked at
Help in getting String Array from arrays.xml file
and this
Retreiving an item from ArrayList at random
But how do I retrieve a random item from my list that is defined in the values.xml file?
解决方案String[] array = context.getResources().getStringArray(R.array.animals_array);
String randomStr = array[new Random().nextInt(array.length)];
Hope this helps.
上一篇:使用PHP在Android上打开实时音频流实时、音频、PHP、Android
下一篇:对于ACTION_IMAGE_CAPTURE没有出现在三星Galaxy Nexus的摄像头意图(4.0.2)出现在、意图、摄像头、ACTION_IM