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 Build a Blog Reader Android App Using Intents to Display and Share Posts Opening a Webpage in the Browser

Louis Sankey
Louis Sankey
22,595 Points

Using Uri.parse and setting an Intent

This one has been giving me some trouble. I am basically supposed to take the urls in the mUrls object and make them open in the web browser when clicked. I believe I am somehow supposed to use the 'position' parameter of the onListItemClick method. And then I am supposed to set my Intent with that data. My attempt is the last three lines down at the bottom in the onListItemClick method. I believe the Intent portion is correct, but I do not know how to get the url's from mUrls. If anyone can help it would be appreciated.

""" import android.view.ListView; import android.content.Intent; import android.net.Uri;

public class CustomListActivity extends ListActivity {

protected String[] mUrls = { "http://www.teamtreehouse.com", "http://developer.android.com", "http://www.github.com" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_list);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mUrls);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
   super.onListItemClick(l, v, position, id);

  String url = mUrls.getString(position);
  Intent intent = new Intent(Intent.ACTION_VIEW);
  intent.setData(Uri.parse(url));
}

}

1 Answer

Jacob Cho
Jacob Cho
7,937 Points

Recall how to get something out of an array.

If you want to get the first item, it'd be mUrls[0]; The position variable gives you an int that is the row position that is clicked. If I click the first row, it'll give me 0, then 1, 2, and so forth.

So to get whatever url is in the row position that I click, I want this code: String url = mUrls[position]; Then your code should be ok, just add startActivity(intent); at the end of your intent code so it actually starts the intent.

Louis Sankey
Louis Sankey
22,595 Points

Thanks Jacob, doing this worked beautifully.