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

Stuck on getting URL

Trying to get the correct syntax for this code challenge: At the bottom, can't seem to get the proper URL from the mUrls array.

Can someone help with this?

import android.os.Bundle; import android.widget.ArrayAdapter; import android.view.View; 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) {
    // Add code here!
  super.onListItemClick(l, v, position, id);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(mUrls));
        startActivity(intent);      
}

}

3 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

You have to tell your code which Url to select by using an array position. The position of the list (which is the same as your array) is passed into the method onListItemClick and is available to you in the variable named position.

You should use intent.setData(Uri.parse(mUrls[position]));

Thank you Daniel!

That was the problem.

Maybe one day I'll get it!

Daniel Hartin
Daniel Hartin
18,106 Points

Just keep at it! I would recommend reading up on array's if you get a moment though as they do crop up all the time and are extremely useful so it is a good idea to have a background knowledge which unfortunately these tutorials only touch on.

When I get this tutorial done, I will indeed go back and read up on some of the functions we went over.