android: How to get item's attributes from its string-array in strings.xml
My dad likes to watch his movies and he has a lot of them. He keeps his discs in several binders but there is no system of cataloging at all.
I thought I'd make an android app to help him find them
So i thought id store the information in a string array in strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, MovieFinderActivity!</string>
<string name="app_name">MovieFinder</string>
<string name="movie_prompt">Choose Movie</string>
<string-array name="all_movies_array">
<item binder="1" page="1">Star Wars Episode 1: The Phantom Menace</item>
<item binder="1" page="1">Star Wars Episode 2: Attack of the Clones</item>
<item binder="1" page="1">Star Wars Episode 3: Revenge of the Sith</item>
<item binder="1" page="1">Star Wars Episode 4: A New Hope</item>
<item binder="1" page="2">Star Wars Episode 5: The Empire Strikes Back</item>
<item binder="1" page="2">Star Wars Episode 6: Return of the Jedi</item>
<开发者_如何学运维;item binder="1" page="2">Dr Zhivago Side A</item>
<item binder="1" page="2">Dr Zhivago Side B</item>
</string-array>
</resources>
then i did this
movie_spinner = (Spinner) findViewById(R.id.search_movie_spinner);
movie_spinner.setAdapter(adapter2);
movie_spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
and this gets called here
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//movie_title is a textview
movie_title.setText( movie_spinner.getSelectedItem() + "");
}
so whenever a movie gets selected in the spinner, the TextView of movie_title changes to the movie's name
What i want to know is how to retrieve the attributes of binder and page from the node item
AFAIK, you are not able to get attribute values for the resources string array.
The way I see it is that you can have the following options
Create an xml file with you data as sherif mentioned. Eg,
<movies> <item binder="1" page="1">Star Wars Episode 1: The Phantom Menace</item> <item binder="1" page="1">Star Wars Episode 2: Attack of the Clones</item> <item binder="1" page="1">Star Wars Episode 3: Revenge of the Sith</item> <item binder="1" page="1">Star Wars Episode 4: A New Hope</item> <item binder="1" page="2">Star Wars Episode 5: The Empire Strikes Back</item> </movies>
You could use any f the avaliable parsers like DOM,SAX to parse this and create an arraylist or object of your choice.
Create json data instead of xml and use GSON to parse to to a usable object.
精彩评论