Android Build a Weather App Hooking Up the Model to the View Plugging in the Data

Kevin Grifo
Kevin Grifo
9,824 Points

Where does the setWeather method come from?

When binding the currentWeather object Ken uses the statement

binding.setWeather(displayWeather);

I was just curious as to where the setWeather method/function is located. What class? I remember Ken stating that a class is automatically generated when enabling and using Java binding called ActivityMainBinding. Is there where this particular method is located? Any help or clarification would truly be appreciated.

Thanks

Ben Deitch
Ben Deitch
Treehouse Teacher

Hey Kevin! I'm not super familiar with data binding, but you should be able to use CMD or CTRL + B on 'setWeather' to jump you to its declaration.

Boban Talevski
Boban Talevski
24,769 Points

It looks like you are right that it's in that automatically generated class ActivityMainBinding.java. I just couldn't find it to see it (CTRL + B doesn't seem to work). The method name is most likely derived from what we set as a variable name in the layout file, i.e. weather, so the generated method gets named setWeather().

1 Answer

Evan Cornish
Evan Cornish
4,692 Points

in the first video, where we were creating the edits to the activity_main.xml file, we made this tag:

<variable
            name = "weather"
            type = "com.example.stormy.CurrentWeather"/>

the library generates the needed method calls. We told it to call it "setWeather" when we chose that name to be weather in that xml file.