【问题标题】:Getting a random item from an xml file从 xml 文件中获取随机项
【发布时间】:2015-12-02 07:09:25
【问题描述】:

我创建了 2 个 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 文件加载为某种数组,然后从中获取随机索引
  • 谢谢丹尼尔。那是我的 B 计划。但在制作应用程序的后期,我想提取随机电影,不仅按类型过滤,还按长度过滤。当我使用数组时,比如here,我不能这样做。或者我可以吗?
  • 如果我是你,我会使用 JSON。想看吗?
  • @ShifarShifz 你有什么特别的 API 吗?
  • noo.......

标签: android xml random


【解决方案1】:

你可以使用 sqlite DataBse,所以你可以用它做任何事情。例如保存名称和流派,然后像这样查询: SELECT * FROM movieWHERE movie.genre = 'drama' ORDERBY RANDOM()

【讨论】:

【解决方案2】:

我通过使用字符串数组制作了一个 xml 文件来采取另一种方式:

<resources>
    <string-array name="comedy">
    <item>Grown Ups</item>
    <item>Grown Ups 2</item>
    <item>Scary Movie</item>
    <item>Superbad</item>
    <item>Zoolander</item>
    <item>Groundhog Day</item>
    <item>Beverly Hills Cop</item>
    <item>Beverly Hills Cop II</item>
    <item>Hangover</item>
    <item>Anchorman</item>
    <item>Pineapple Express</item>
    <item>Happy Gilmore</item>
    <item>We are the Millers</item>
    <item>Horrible Bosses</item>
    <item>Horrible Bosses 2</item>
    <item>Meet the Parents</item>
    </string-array>

    <string-array name="action">
    <item>Man in Black</item>
    <item>Man in Black II</item>
    <item>Man in Black III</item>
    <item>Terminator</item>
    <item>Die Hard</item>
    <item>Back to the Future</item>
    <item>The Bourne Identity</item>
    <item>The Avengers</item>
    <item>Jurassic Park</item>
    <item>Jurassic World</item>
    <item>The Dark Night</item>
    <item>The Matrix</item>
    <item>Mission: Impossible</item>
    <item>Mission: Impossible II</item>
    <item>Mission: Impossible - The Phantom Pain</item>
    <item>Kill Bill - Volume 2</item>
    <item>Apocalypto</item>
    <item>Aliens</item>
    <item>Apocalypse Now</item>
    <item>Logans Run</item>
</string-array>
</resources>

现在我可以轻松获得一个数组,然后使用随机生成器。

到目前为止,这就足够了。

但在稍后制作应用程序的过程中,我不仅要按流派过滤,还要按长度过滤。 这就是我尝试使用第一个溃败的原因,如我最初的问题所示。 如果有人对这项工作提出建议,那就太好了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2015-01-06
    • 2012-06-30
    • 1970-01-01
    • 2017-12-22
    • 2017-08-30
    • 2021-08-08
    相关资源
    最近更新 更多