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 Adapting Data for Display in a List Filling Our String Array and Creating the Adapter

stage 4 challenge task 2

After your for loop, declare an ArrayAdapter variable named 'adapter' and initialize it with its constructor. Remember that ArrayAdapter is a generic, meaning that the type of the items in the array needs to be specified in angle brackets. The constructor takes three parameters: 1) the context (use 'this'), 2) the layout (use android.R.layout.simple_list_item_1) and 3) the array to adapt.

CodeChallenge.java
/*** This code is an excerpt from a class that extends ListActivity ***/

JSONArray jsonVideos = jsonData.getJSONArray("videos");
String[] titles = new String[jsonVideos.length()];

 for (int i = 0; i < jsonVideos.length(); i++) {
    JSONObject posts = jsonVideos.getJSONObject(i); 
   titles[i] = posts.getString("title");}

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







// Write for loop here!
;
data.json
{
  "id":"PL4C60560903672EF7",
  "author":"gotreehouse",
  "title":"Just For Fun",
  "description":"Just plain old funny videos.",
  "videos":[
    {
      "id":"UQzBS4eGtl0",
      "uploaded":"2013-02-06T15:37:37.000Z",
      "uploader":"gotreehouse",
      "category":"Education",
      "title":"Treehouse Just Landed!"      
    },
    {
      "id":"jQNXtyoyHic",
      "uploaded":"2012-04-25T14:51:19.000Z",
      "uploader":"gotreehouse",
      "category":"Entertainment",
      "title":"Meet Mike the Frog | Treehouse"
    },
    {
      "id":"fHpAQadEZIs",
      "uploaded":"2013-02-06T15:37:31.000Z",
      "uploader":"gotreehouse",
      "category":"Education",
      "title":"Getting Started with HTML5 JavaPress"
    },
    {
      "id":"PKFPW3do6ks",
      "uploaded":"2013-02-06T15:37:41.000Z",
      "uploader":"gotreehouse",
      "category":"Education",
      "title":"EYC Outtakes with Mike the Frog"
    }
  ]
}

1 Answer

The key part which isn't right is the array you are adapting. You are adapting the array: titles

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