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