Associating ULRs with items in a string array

Go To StackoverFlow.com

0

I want to associate URLs with items in a string array so that when an item in a ListView is selected data is scraped from the URL. I was wondering could the URL be added as metadata in the strings.xml file? Does anyone have any suggestions as to how to proceed?

2012-04-04 00:00
by shinokamparos


0

Strings.xml isn't the right place. You COULD define the URLs, one at a time, in strings.xml, like this:

<string name="url1">http://foo1.com</string>
<string name="url2">http://foo2.com</string>
// etc

And then create an array at runtime, dumping all these values in one-by-one. But that would suck.

Instead, create an xml file in res/values/, name it whatever you want (for instance, urlvals.xml), and populate it like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="urls">
        <item>http://foo1.com</item>
        <item>http://foo2.com</item>
    </array>
</resources>

Then, in code, reference it in the following way:

String[] myUrls = getResources().getStringArray(R.array.urls);
2012-04-04 00:26
by Alexander Lucas


0

If you want an array of string resources, you can either:

1) add the strings in strings.xml (as per @Alexander's answer), and reference them by name in an array in values/yourresourcename.xml as per here.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="myLinks">
        <item>@string/link1</item>
        <item>@string/link2</item>
    </array>

2) add them directly the string array (verbatim) like @Alexander suggests

How you associate them with the elements in the ListView is up to you. I would suggest (if the content is static) creating a matching string array for (e.g.) the string you want to actually display (as opposed to the link itself).

2012-04-04 00:33
by Jon O
Ads