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 Build a Weather App Hooking Up the Model to the View Plugging in the Data

Where does the weather.formatted time attribute come from?

I've watched the video's a couple of times now. but formattedTime is never declared anywhere (only a method 'getFormattedTime'). So how does the binding know what formattedTIme is?

1 Answer

Ken Alger
Ken Alger
Treehouse Teacher

Remi -

This is a great question.

If we take a look inside activity_main.xml we have defined a variable, weather that accesses the CurrentWeather.java class. This is how we are able to get data from there with things like weather.temperature. The data binding library translates the Getters (getTime(), getTemperature(), etc) from the class. Since we defined our own function to return time in a specific format in getFormattedTime(), the data binding library allows us to get that information in the layout with our defined variable name and the data item we want, with dot notation. Therefore, we can access our formatted time value in the layout with weather.formattedTime.

I hope that makes sense. Please post back if it does not.

Best wishes,
Ken W. Alger