0

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?

3
  • -1 because "it's not working" is the one sentence you should never include in your question. Describe the problem with technical terms and show compiler output/stack-traces Commented Oct 25, 2012 at 21:14
  • I am new here. I alread fix the problem. Thank you. Commented Oct 25, 2012 at 21:29
  • @LukasKnuth the question was finally edited. Please reconsider you vote. Thank you again. Commented Feb 12, 2015 at 19:20

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.