我创造了2个XML文件的XML文件:获得一个随机的项目从
RES/XML/comedy.xml
<comedy>
<item title="Grown Ups" length="90min"/>
<item title="Grown Ups 2" length="90min"/>
<item title="Scary Movie" length="90min"/>
<item title="Superbad" length="90min"/>
<item title="Zoolander" length="90min"/>
<item title="Groundhog Day" length="90min"/>
<item title="Beverly Hills Cop" length="90min"/>
<item title="Beverly Hills Cop II" length="90min"/>
<item title="Hangover" length="90min"/>
<item title="Anchorman" length="90min"/>
<item title="Pineapple Express" length="90min"/>
<item title="Happy Gilmore" length="90min"/>
<item title="We're the Millers" length="90min"/>
<item title="Horrible Bosses" length="90min"/>
<item title="Horrible Bosses 2" length="90min"/>
<item title="Meet the Parents" length="90min"/>
RES/XML/action.xml
<action>
<item title="Man in Black" length="90min"/>
<item title="Man in Black II" length="90min"/>
<item title="Man in Black III" length="90min"/>
<item title="Mission: Impossible" length="90min"/>
<item title="Mission: Impossible II" length="90min"/>
<item title="Mission: Impossible - The Phantom Pain" length="90min"/>
<item title="Terminator" length="90min"/>
<item title="Die Hard" length="90min"/>
<item title="Back to the Future" length="90min"/>
<item title="The Bourne Identity" length="90min"/>
<item title="Marvel's The Avengers" length="90min"/>
<item title="Jurassic Park" length="90min"/>
<item title="Jurassic World" length="90min"/>
<item title="The Dark Night" length="90min"/>
<item title="The Matrix" length="90min"/>
<item title="Kill Bill - Volume 2" length="90min"/>
<item title="Apocalypto" length="90min"/>
<item title="Apocalypse Now" length="90min"/>
<item title="Aliens" length="90min"/>
现在我想从这些文件中抽出一部随机电影。
有没有办法直接做到这一点?或者我必须用XmlPullParser
(如here)拉出所有项目,然后使用随机生成器?
旁注
1:我不能完全肯定,如果我做了那些xml
文件的方式是理想的。我只是将自己定位于链接中的示例。如果你有更好的想法,请告诉我。
2:最终目标是在用户选择一个或多个流派之后在应用中显示随机电影。
您将需要加载xml文件作为某种数组,并且只是从中得到一个随机索引 –
感谢Daniel。 这是我的计划B. 但后来在制作应用程序的过程中,我想抽出随机电影,不仅按流派过滤,还要按照长度过滤。 当我使用数组,像[这里](http://stackoverflow.com/questions/11600001/how-to-get-a-random-value-from-a-string-array-in-android)我可以不这样做。或者我可以吗? – Johannes
如果我是你,我会使用JSON。想看到吗? – theapache64