Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Opening a Webpage in the Browser (2 of 2) Help Needed

The member variable 'mUrls' is filled with a few URLs. In the 'onListItemClick()' method, declare and start an Intent that causes the URL tapped on to be viewed in the browser. Hint: The String URL needs to be set as the data for the Intent as a Uri object.

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
          super.onListItemClick(l, v, position, id);
          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setData(Uri.parse(mUrls);
          startActivity(intent);
    }

Been stuck for a while now. Not sure how to make it work.

4 Answers

I figured it out. For anyone who needs help:

            @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
      super.onListItemClick(l, v, position, id);
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setData(Uri.parse(mUrls[position]));
      startActivity(intent);
} 
ms2030
ms2030
2,973 Points

Thanks Tom for posting your answer. That solved my issue about what to do with the position.

Seye Kuyinu
Seye Kuyinu
2,581 Points

Thanks for this! Helped me

Yachthaven Support
Yachthaven Support
3,121 Points

Joshua, it works because the mUrls array of strings can't be parsed as a single Url, and so as the onClickListener class brings in position as a variable, by setting the specific string in the array of mUrls (using mUrls[position]) you are selecting just the one that was clicked.

Joshua Lopez
Joshua Lopez
8,972 Points

I'm confused on why that works.

Thanks, Tom