Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Lists and Adapters (2015) Updating the Data Model Using JSONArray

Finally, write a 'for' loop that loops through jsonShows. In each step of the loop, use getJSONObject(int index) to get

Challenge Task 2 of 2

Finally, write a 'for' loop that loops through jsonShows. In each step of the loop, use getJSONObject(int index) to get a JSONObject from the array. Then use the Log.i() method (with "CodeChallenge" as the tag) to write the show title. Please help am stuck on this challenge. Am getting this error when l run the code below Bummer! Don't forget to use "CodeChallenge" as the 1st parameter of the Log.i() method!

for (int i = 0; i < jsonShows.length(); i++) { JSONObject shows = jsonShows.getJSONObject(i); Log.i("CodeChallenge"); }

CodeChallenge.java
// JSONObject was loaded from data.json
JSONArray jsonShows = jsonData.getJSONArray("shows");
 for (int i = 0; i < jsonShows.length(); i++) {
    JSONObject shows = jsonShows.getJSONObject(i);
    Log.i("CodeChallenge");
  }

1 Answer

Hi there,

The for loop would look like:

JSONArray jsonShows = jsonData.getJSONArray("shows");
for (int i = 0; i < jsonShows.length(); i++){
  JSONObject jObj = jsonShows.getJSONObject(i);
  Log.i("CodeChallenge", jObj.getString("title"));
}

Your output isn't what the challenge expects - it wants the TAG and the show title.

Hope that helps.

Steve.

Tuan Zen
Tuan Zen
6,882 Points

Hi Steve,

Thanks for your answer!! I'd like to know why u don't need to pass in a return statement for this for loop, 'cause usually there would be a runtime error if there isnt one?

Hi there,

A for loop doesn't need a return to work. This challenge is expecting an output in the form of an entry in the Log - that's achieved within the loop itself so there's no need to specifically return anything.

Why would a lack of return create a runtime error?

Steve.