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.

/*** 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!
  "title":"Just For Fun",
  "description":"Just plain old funny videos.",
      "title":"Treehouse Just Landed!"      
      "title":"Meet Mike the Frog | Treehouse"
      "title":"Getting Started with HTML5 JavaPress"
      "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);