Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
It's time to do more than just write our weather data to the log. Let's use all the views we've added to display real weather data.
-
0:00
Our next step is to update our data model.
-
0:03
In current weather, we need to create two different constructors for our class.
-
0:07
Fortunately, Android Studio can generate those for us.
-
0:11
Go to Code > Generate > Constructor.
-
0:16
As I mentioned, we need two different constructors in our class.
-
0:20
So we'll generate the first one that will accept no arguments.
-
0:23
We just need to click on Select None.
-
0:26
Give some space.
-
0:28
We'll generate our second one.
-
0:32
Instructor, this one, we'll select all the variables.
-
0:38
And let's clean this up a little bit.
-
0:40
The basic constructor is used in main activity
-
0:43
in our get current details method.
-
0:45
The second constructor will be used when we bind our data.
-
0:49
And these dual constructors are a great example
-
0:52
of constructor overloading in Java.
-
0:55
For more information about this, check the teachers notes.
-
0:58
Alright, with our model updated,
-
1:00
let's take another look at the data binding documentation.
-
1:04
If we look here at Binding Data, we see that the easiest means for
-
1:09
creating the bindings is to do it while inflating.
-
1:13
I'm all about doing things the easiest way possible.
-
1:16
So let's follow this example.
-
1:18
So we need to go back into onCreate and adjust our set content view statement.
-
1:26
Back into MainActivity, so here we're at SetContentView.
-
1:33
ActivityMainBinding, Binding, do our imports.
-
1:41
It's gonna be a DataBindingUtil,
-
1:46
convert context, MainActivity.this.
-
1:52
Let's drop that down to a new line.
-
1:54
The documentation says that we need to bind our data to our new binding variable.
-
1:59
Well, we don't have any data at this point in onCreate.
-
2:02
We do, however, have it down in onResponse,
-
2:05
where we're making a call to getCurrentDetails.
-
2:08
So let's bind our data at that point.
-
2:11
So down here in onResponse, We'll
-
2:16
make a new CurrentWeather, Object, we'll call it displayWeather.
-
2:25
Will be new CurrentWeather.
-
2:30
Here's where we'll use our new constructor.
-
2:32
We'll pass in the information from our CurrentWeather variable as called for
-
2:36
in the constructor.
-
2:44
GetIcon.
-
2:48
GetTime.
-
2:54
GetTemperature.
-
2:59
GetHumidity.
-
3:02
GetPrecipChance.
-
3:07
Get our summary.
-
3:11
And get our time zone.
-
3:15
Fantastic, now we need to use this new displayWeather with our binding
-
3:20
variable to bind our data.
-
3:22
We can do binding.setWeather, and we'll pass in displayWeather.
-
3:29
Since we're using binding from within an inter class,
-
3:32
we need to make sure that it's declared final.
-
3:37
And it is, thank you Android Studio.
-
3:41
Since we already have one data point, our summary, set up in our layout,
-
3:44
let's run this and make sure our data is coming through.
-
3:53
And it's changed in our app, and if we look in Logcat and our JSON data.
-
4:05
It's the same there.
-
4:07
That's awesome.
-
4:08
Let's head over to our activity main XML file and update the rest of our fields.
-
4:16
Before we get started on this process,
-
4:18
let's think a little bit about what our text field is wanting.
-
4:22
It's expecting string values, so we're going to have to make a decision.
-
4:26
Do we want to alter our model and store data as strings?
-
4:30
Or do we want to convert them to strings here in the XML?
-
4:34
I think it makes more sense to keep our data in its native state in our model.
-
4:38
With that in mind, let's look at our temperature value here.
-
4:43
We want to update this static value to our data value.
-
4:48
We'll do @ with our curly braces.
-
4:52
We wanna convert it to a string and get the value of,
-
5:00
weather.tempeture.
-
5:03
Next up is our time value.
-
5:09
Here we want to use our formatted time and
-
5:12
add it into the middle of our statement of at such and such a time it will be.
-
5:17
To concatenate text inside an Android text value,
-
5:20
we need to use the grave accent character instead of quotes.
-
5:25
That character is found above the Tab key on most keyboards.
-
5:29
Inside our time value then, our string will look like this.
-
5:38
Grave accent At space and we'll concatenate,
-
5:46
String.valueOf.
-
5:51
Weather.formattedTime, and then the rest of our string characters.
-
5:56
It will be and another grave accent.
-
6:00
Let's go down to humidity.
-
6:04
Humidity value, String,
-
6:11
value of, weather.humidity.
-
6:21
And for precipitation,
-
6:23
we need to concatenate that percent sign in there as well.
-
6:40
Add in a grave accent with a percent.
-
6:43
Let's run it again and see how that looks.
-
6:53
Well, there's our data, but our temperature has a decimal point in there,
-
6:57
and our precipitation value is a decimal.
-
7:01
I think just having the whole number is better, and
-
7:03
it fits with our design markups also.
-
7:06
Again, it's decision time for how we handle that.
-
7:09
We can do it in our model or in our XML.
-
7:12
We can certainly handle it in either place but changing it in the XML allows for
-
7:17
more flexibility if we decide to expand our app to another activity and
-
7:22
need to use the data elsewhere.
-
7:24
Therefore, let's update the temperature and precipitation values in there.
-
7:28
We can use the round method from Java's Math class to round our values.
-
7:33
For temperature, then, we can go back up here to temperature.
-
7:44
We'll do Math.round, weather.Temperature.
-
7:56
And for precipitation,
-
7:58
we want to multiply it by 100 to make the decimal into a percentage as well.
-
8:08
Math.round.
-
8:18
Let's run it again.
-
8:26
Very nice, our text data is coming through and updating our view.
-
8:30
This is looking great.
-
8:32
We lost all of our default text though, in this process,
-
8:35
making it a bit of a challenge in our design pane.
-
8:38
Let's add the default values back in, not only for ourselves during the development
-
8:44
process, but also so our users don't have to stare at a blank screen.
-
8:48
So we'll come here, Default, 50%.
-
9:00
For humidity, Default,
-
9:09
0.88.
-
9:19
Default here will be at 5 o'clock PM, it will be.
-
9:32
For temperature, we'll do a default, 100.
-
9:41
And we'll add one for the summary as well.
-
9:52
Default is stormy with a chance of meatballs.
-
10:00
Remember to use the grave accent character there to be able to have spaces in our
-
10:04
strings.
-
10:05
This is looking nice, but what about that weather icon?
-
10:08
Let's see about updating that next.
You need to sign up for Treehouse in order to download course files.
Sign up