views:

104

answers:

2

I have 2 items that are related to each other that need to be accessed separately. The title of a book and how many pages it has. I have made an array for the titles, but what is the best way to link the number of pages in that book to that title?

I was thinking of the following but I don't see how to access the page count.

<string-array name="books">
    <item pages="428">"book1"</item>
    <item pages="599">"book2"</item>
    <item pages="204">"book3"</item>
</string-array>
+1  A: 

Wouldn't it be better this?

<books>
    <book>
        <pages>428</pages>
        <title>book1</title>
    </book>
    <book>
        <pages>599</pages>
        <title>book2</title>
    </book>
    <book>
        <pages>204</pages>
        <title>book3</title>
    </book>
</books>

The XPath expression to get the number of pages will be

/books/book[title='book1']/pages

If you are worry about size, then this:

<books>
    <book pages="428" title="book1"/>
    <book pages="599" title="book2"/>
    <book pages="204" title="book3"/>
</books>

And the XPath expression:

/books/book[@title='book1']/@pages
Alejandro
A: 

You could go with XPath but IMHO it would be overkill.

To me it sounds like you need a Map of some sort to keep track of the pages. I would personally avoid the use of strings array (since I don't think there is anyway to reference an attribute for an element) as follows:

// create HashMap to write pages to
Map<String, Integer> bookPageMapping = new HashMap<String, Integer>();
bookPageMapping.put("book1", 428);
bookPageMapping.put("book2", 599);
bookPageMapping.put("book3", 204);

You could also, put the book literals into the strings.xml as follows and reference them that way as well:

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MultiArray</string>
    <string name="book1">book1</string>
    <string name="book2">book2</string>
    <string name="book3">book3</string>
</resources>

Then you would reference the files in your code as follows:

// create HashMap to write pages to
Map<String, Integer> bookPageMapping = new HashMap<String, Integer>();
bookPageMapping.put(getString(R.string.book1), 428);
bookPageMapping.put(getString(R.string.book1), 599);
bookPageMapping.put(getString(R.string.book1), 204);

Or you could go with the strings array:

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">MultiArray</string>
    <string-array name="books">
        <item>book1</item>
        <item>book2</item>
        <item>book3</item>
    </string-array>
</resources>

Then populate the hashmap accordingly:

// get the String array from resources
Resources res = getResources();
String[] books = res.getStringArray(R.array.books);

// create HashMap to write pages to
Map<String, Integer> bookPageMapping = new HashMap<String, Integer>();
bookPageMapping.put(books[0], 428);
bookPageMapping.put(books[1], 599);
bookPageMapping.put(books[2], 204);

For more information see the Android Developer's site and do a search for String Resources or Map. I would have directly posted those links but since I am a new user, I cannot post more than 1 hyperlink. BTW, this is my first Stack Overflow post so be gentle :)

Noel