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 Android Lists and Adapters (2015) Updating the Data Model Adding a New Data Model Object

Sean Gallagher
Sean Gallagher
2,054 Points

How to parse the Hourly Data using both summary keys

Under the Hourly data I wasn't clear how the code is able to discern the difference between the 2 summaries - one inside the "data" array and one outside, I didn't see us specifying to look inside the data array for summary when we set up the Hour class or referred to it in the Forecast class.

I'm trying to figure out if you could refer to both using this example but it doesn't look like you can use getJSONArray.

Inside Hourly class:

private String mSummary; private Array mSummaryData[];

getters and setters for both

Inside the MainActivity in Private Current Weather: private CurrentWeather getCurrentDetails(String jsonData) throws JSONException { ... currentweather.setSummary(dinosaurs.getString("summary")); currentweather.setSummaryData(dinosaurs.getJSONArray("summary"));

Is there a way to parse/get both? SHould I set the array to a string?

3 Answers

Sean Gallagher
Sean Gallagher
2,054 Points

Oh that's a great way to solve it - thanks!. And the questions I had about JSONArray are actually covered in the Lists And Adapters class - chapter on Intro to JSONArray: http://teamtreehouse.com/library/android-lists-and-adapters/updating-the-data-model/introducing-jsonarray

.

private Current getCurrentDetails(String data) throws JSONException {
        JSONObject obj = new JSONObject(data);
        String timezone  = obj.getString("timezone");
        JSONObject cur = obj.getJSONObject("currently");

        Current mCurrent = new Current();
        mCurrent.setHumid(cur.getDouble("humidity"));
        mCurrent.setPercChance(cur.getDouble("precipProbability"));
        mCurrent.setSummary(cur.getString("summary"));
        mCurrent.setTemp(cur.getDouble("temperature"));
        mCurrent.setTime(cur.getLong("time"));
        mCurrent.setIcon(cur.getString("icon"));
        mCurrent.setTimeZone(timezone);

        return mCurrent;
    }
Sean Gallagher
Sean Gallagher
2,054 Points

Yes thank you. I see how that works. One thing I failed to mention, in my sample data I made Temperature an array for Celsius and Fahrenheit. So is it possible to just change temperature to:

In MainActivity mCurrent.temperature = cur.getJSONArray("temperature");

Then in Current: Add a private Temperature[] with getters & setters? Or do I need to create a new class for this array the way Ben did for Hours & Days?

I have a setter in Current for temp:

 mCurrent.setTemp(cur.getDouble("temperature"));

My thought is that you would change the function setTemp in Current.java to take a boolean variable for Celsius. For example have a variable in Main.java isCelsius set is in main, possibly based on location (a location that Celsius is generally accepted, for example anywhere in the world other that US) and pass that into the setTemp function:

 mCurrent.setTemp(cur.getDouble("temperature"), isCelsius);

And in the function in Current.java have a check and do the conversion from one to the other. In this case, fahrenheit to celsius because that is how the data comes from forcast.io:

public int setTemp(double temp, boolean isCelsius) {
  if(isCelsius) {
      mTemp = (int)Math.round(((mTemp - 32) * 5) / 9)
  } else {
        mTemp = (int)Math.round(temp);
    }

So that later on when you call getTemp() it will return the value you just set.

I got this formula from http://www.manuelsweb.com/temp.htm so I don't know if this conversion is 100% correct, but that would be the idea.