Associating ULRs with items in a string array

Go To


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


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

<string name="url1"></string>
<string name="url2"></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"?>
    <array name="urls">

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

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


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"?>
    <array name="myLinks">

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