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

please assist with task 1 n 2

dd

1 Answer

I want to avoid giving you the direct answer so i'll just explain how each is done in the context of the weather app. Task 1 - So, for task one you are required to pull a JSONArray from the already loaded jsonData object. The jsonData object has all the information you need so all you have to do is use it's method getJSONArray(). This method takes a string parameter which is the key for data you need. In the weather app, you needed to get the hours data, so the way it is done is as follows:

JSONArray jsonHours = jsonData.getJSONArray("hours"); //We are basically telling jsonData to give use the array, 
                                                                         //we want by using the key "hours".

Task 2 - Ok, so now we have an array, but we need to get specific data from it. Since we need to go through each item in the array a for loop works best in this case. So for the hours array we need to get each one. In the lesson Ben stores the information in an Hours object array, but since you are having trouble with the code challenge, I will make it display all the temperatures.

for(int i =0; i<jsonHours.length();i++){ //so here we are telling the loop to go until the length of the array.

JSONObject hour = jsonHours.getJSONObject(i); // here we are getting the appropriate JSONObject based on index.
Log.i("TAG", hour.getDouble("temperature")+"") // we create a log statement to print out the temperature of the, 
                                                                     //hour we got from the array.

I hope this helped! Feel free to ask anymore questions! :)