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

Issue with Filling our String Array for Android Blog Reader

Hello,

I can't get past the first part of the Code Challenge for the Filling Our String Array and Creating Adapter. I think I'm on the right track but maybe not... Any help would be greatly appreciated. Thanks in advance!

/*** 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 videos = jsonVideos.getJSONObject (i);
  String title = videos.getString("title");
  title = Html.fromHtml(title).toString();
  jsonDataTitles [i] = title;
}

6 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

There are two things wrong with your code. (Not exactly wrong, but in terms of our code challenge engine.) :)

  1. The name of the titles array is "titles", but you are using "jsonDataTitles"
  2. You can't actually use this line in the challenge. :( It's not a perfect Android environment (yet)! title = Html.fromHtml(title).toString();

Doh! Thanks so much for pointing that out. I am really enjoying the classes, even the code challenges that really challenge my brain…

I'm stumped on this as well. Here is my code (I have no idea what I am doing wrong):

for(int i = 0; i < jsonVideos.length(); i++) {
    JSONObject videos = jsonVideos.getJSONObject(i);
    String title = videos.getString("title");  
    Titles[i] = title;
}
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Connor Nesbitt, in your case, a quick glance looks like a simple typo with a capital "T" in "titles" (it should be lowercase). Here's the line where it's declared:

String[] titles = new String[jsonVideos.length()];

So when you assign title you need to type the name of the array the exact same way. Hopefully that's all it is!

Yup, that was my issue. Thank you so much for your help Ben!

I can't get step 2, here's my code:

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

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

//All the emulator tells me is the set the <String> before ArrayAdapter. Think that's what i've done.

?????

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Sam Donald,

First - sorry that the error messages aren't more helpful! At this point we can't capture exact syntax errors, but that's something we'd like to improve in the future.

Couple of things:

  1. Your code to set adapter is a little off. It's almost right. You just need to call the constructor, which ends with the parenthesis. By putting the curly braces you are trying to turn this into some sort of method definition, which doesn't apply here.
  2. You are using the wrong array for the adapter. It's adapting a String array (hence String in the angle brackets), but jsonVideos is a JSONArray. Look for the array of Strings (hint: String[]) and use that instead.

Hope this helps!

Yes thank you!!!

Ben > sam :)