0

I have created 2 xml files:

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"/>

Now I want to pull a random movie out of each of these files.

Is there a way I can do that directly? Or do I have to pull all items out with XmlPullParser (like here) and then use the Random Generator?

Side notes

1: I'm not entirely sure if the way I made those xml files is ideal. I just oriented myself on the sample in the link. If you have better ideas, please tell me.

2: The end goal is to display a random movie in an app after the user selected one or more genres.

Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
  • You will need to load the xml files as some sort of an array, and simply get a random index out of it – Daniel Mendel Sep 06 '15 at 21:45
  • Thanks Daniel. That is my plan B. But later in the process of making the app, I want to pull out random movies, filtered not only by genre, but also by length. When I work with arrays, like [here](http://stackoverflow.com/questions/11600001/how-to-get-a-random-value-from-a-string-array-in-android) I can't do that. Or can I? –  Sep 07 '15 at 07:42
  • If i were you, i'd be using JSON. Want to see that ? – theapache64 Sep 07 '15 at 19:24
  • @ShifarShifz Do you have a particular API in mind? –  Sep 07 '15 at 22:34
  • noo.................. – theapache64 Sep 08 '15 at 11:15

2 Answers2

0

You can use sqlite DataBse, so you can do everything with that. For example save the name and Genre, then query sth like this: SELECT * FROM movieWHERE movie.genre = 'drama' ORDERBY RANDOM()

Dara
  • 28
  • 5
  • The only way I know how to create a sqlite database with data, that is already given (like here, the movie lists) is with and xml file. –  Sep 07 '15 at 07:30
  • this tutorial can be useful for sqlite database: (http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/) – Dara Sep 07 '15 at 13:52
0

I took an alternative rout by making an xml file with string arrays:

<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>

Now I can easily get an array and then use the Random Generator.

So far that's sufficient.

But later in the process of making the app I'll have to filter not only by genre, but also by length. That's why I tried to use the first rout, as shown in my initial question. It would be great, if anyone has a suggestion to make that work!