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
Nanette Keir
17,666 PointsIssue 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
Treehouse TeacherThere are two things wrong with your code. (Not exactly wrong, but in terms of our code challenge engine.) :)
- The name of the titles array is "titles", but you are using "jsonDataTitles"
- You can't actually use this line in the challenge. :( It's not a perfect Android environment (yet)!
title = Html.fromHtml(title).toString();
Nanette Keir
17,666 PointsDoh! Thanks so much for pointing that out. I am really enjoying the classes, even the code challenges that really challenge my brain…
Connor Nesbitt
4,824 PointsI'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
Treehouse TeacherConnor 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!
Connor Nesbitt
4,824 PointsYup, that was my issue. Thank you so much for your help Ben!
Sam Donald
36,305 PointsI 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
Treehouse TeacherHi 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:
- Your code to set
adapteris 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. - You are using the wrong array for the adapter. It's adapting a String array (hence
Stringin the angle brackets), butjsonVideosis a JSONArray. Look for the array of Strings (hint:String[]) and use that instead.
Hope this helps!
Sam Donald
36,305 PointsYes thank you!!!
Ben > sam :)