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

Robert Garza
Robert Garza
4,881 Points

On Android Development stage 5 I'm suck on how to set the data from my Intent to mUrls.

I understand why this works in the video and the app. At least I believe I do. So when we do the try block

            JSONArray jsonPosts = mBlogData.getJSONArray("posts");
            JSONObject jsonPost = jsonPosts.getJSONObject(position);
            String blogUrl = jsonPost.getString("url");
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(blogUrl));
            startActivity(intent);

We are pulling the URL from the the json data based off where we click. My question in regards to the code challenge is this -

How can we pull the URL when there is no JSON data? The mUrls array is a string and I'm not exactly sure how to do this. Is there a way to pull an item from the array and set my intent to it? I'm trying to use the following

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

But of course I'm getting an error stating that I cannot setData with a String[] data type. I'm stuck.

// CustomListActivity.java
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.getData(Uri.parse(mUrls));
      startActivity(intent);
    }
}

1 Answer

Harry James
Harry James
14,780 Points

Yes, you should be able to pull items from an array completely fine! I have never tried this in the context of a URI/URL myself but believe that this will work (I don't see why it wouldn't).

Change this line:

intent.setData(Uri.parse(mUrls);

to this:

intent.setData(Uri.parse(mUrls[0]);

Here, replace the 0 with the number in the array that you want to set the data as.

Remember! Arrays start from 0 so, if you leave it as 0, it will use the teamtreehouse.com website!

To clarify: You can pull specific items from an array using this syntax: variable[positionInArray].

Hope it helps and, if you have any more questions, give me a shout! :)

Robert Garza
Robert Garza
4,881 Points

I can't believe I didn't think about pulling a single item from the array instead of pulling the entire array. Thank you so much! It worked.

Harry James
Harry James
14,780 Points

Woohoo!

Glad that it fixed the problem for you and great to see that you're trying something outside of the course itself! It's a fantastic way to learn so big credit to you for giving it a go! :)