androidevil
androidevil

Reputation: 9171

How to get a string from a string-array?

In my app I have a ListView which is populated with URLs from a string-array. When the user selects some item in this ListView, the app should open a WebView and load the selected URL.

The string-array is like this:

<string-array name="app_urls">
    <item>http://www.google.com</item>
    <item>http://www.android.com/</item>
    <item>http://www.youtube.com</item>
</string-array>

I am trying to get the string value of the url this way:

Resources res = getResources();
String[] url = res.getStringArray(R.array.app_urls);
webView = new WebView(view.getContext());
webView.loadUrl("http://" + url);

How can I correctly get the url value?

Upvotes: 0

Views: 163

Answers (2)

Josh Bibb
Josh Bibb

Reputation: 477

I dont know much about android but the last line you probably want something like

webView.loadurl(url[index of the url you want to use]);

because A) you already have the http://'s in your array definition so it would be redundant your way,

and B) as the above poster said you have to reference your array items by index. Think of an array like a train, where each 'index' is a car holding some data. Arrays are 0-indexed so the first slot is [0], 2nd is [1] and so on.

So if you wanted to get google out of your frst array youd call bookmark_urls[0], and if you wanted offspot youd say bookmark_urls[2].

I'm not sure why you define your string array then declare another string array and (as near as i can tell) make it equal to the first though. Cant you just reference 'bookmark_urls[]' throughout?

Upvotes: 1

Yngwie89
Yngwie89

Reputation: 1217

you have to access the element in the array.. for instance url[0] if the first element and so on.... you can't call an array in that way....

Upvotes: 0

Related Questions