1 00:00:00,230 --> 00:00:03,210 We just finished setting up our menu to give users a choice in 2 00:00:03,210 --> 00:00:05,480 how much data they'd like to see. 3 00:00:05,480 --> 00:00:08,690 But while the menu is there, the functionality isn't. 4 00:00:08,690 --> 00:00:10,940 We still need to create the showLast function. 5 00:00:10,940 --> 00:00:12,865 So let's use Alt+Enter to create it. 6 00:00:16,513 --> 00:00:19,820 And I'm going to change the variable name to n. 7 00:00:19,820 --> 00:00:23,600 Now that we've got our showLast function, we just need to make it work. 8 00:00:23,600 --> 00:00:27,630 Which means in the onDraw function, instead of drawing all of our data, 9 00:00:27,630 --> 00:00:30,270 we need to be able to draw just a subset of it. 10 00:00:30,270 --> 00:00:34,090 Let's start by creating a new field to hold a subset of our data. 11 00:00:34,090 --> 00:00:37,895 Let's use Cmd or Ctrl+D to duplicate our data line, and 12 00:00:37,895 --> 00:00:40,363 then just change the name to subset. 13 00:00:43,712 --> 00:00:48,560 Then inside onDraw, let's use the subset of our data instead of all the data. 14 00:00:49,630 --> 00:00:55,570 So let's replace data with subset, data with subset, and data with subset. 15 00:00:55,570 --> 00:00:57,329 Finally, in the constructor, 16 00:00:57,329 --> 00:01:00,358 let's initialize our subset to be equal to our data. 17 00:01:00,358 --> 00:01:05,656 Subset equals data. 18 00:01:05,656 --> 00:01:07,420 Nice. 19 00:01:07,420 --> 00:01:10,750 Now that we're using a subset of our data instead of all the data, 20 00:01:10,750 --> 00:01:14,150 we just need to update the subset and the showLast function. 21 00:01:14,150 --> 00:01:18,538 So inside the showLast function, 22 00:01:18,538 --> 00:01:25,346 let's just set subset equal to data.subList(0, 23 00:01:25,346 --> 00:01:28,590 n), and there we go. 24 00:01:28,590 --> 00:01:33,280 All that's left is implementing the zero parameter version for the all data option. 25 00:01:33,280 --> 00:01:35,660 So let's just copy and paste our showLast function. 26 00:01:39,152 --> 00:01:40,464 And then remove the parameter. 27 00:01:42,832 --> 00:01:47,697 Then inside this function, let's just call the other 28 00:01:47,697 --> 00:01:53,070 showLast function while passing in data.size for n. 29 00:01:53,070 --> 00:01:55,803 Awesome, now let's run the app and see what happens. 30 00:01:57,968 --> 00:02:01,904 We've got our chart, and we've got our options. 31 00:02:01,904 --> 00:02:05,450 And if we click on one, nothing happens. 32 00:02:05,450 --> 00:02:07,700 But don't worry, I know what we did wrong. 33 00:02:07,700 --> 00:02:11,670 As it turns out, drawing is kind of an expensive operation. 34 00:02:11,670 --> 00:02:15,200 An Android does its best to avoid any unnecessary drawing. 35 00:02:15,200 --> 00:02:17,640 So whenever you make a change to one of your views, 36 00:02:17,640 --> 00:02:22,550 we need to tell Android about it, which we do by using the invalidate method. 37 00:02:22,550 --> 00:02:27,710 Calling invalidate on a view signals to Android that this view is no longer valid 38 00:02:27,710 --> 00:02:29,390 and needs to be redrawn. 39 00:02:29,390 --> 00:02:31,180 Also, along with invalidate, 40 00:02:31,180 --> 00:02:34,030 you should probably know about the request layout function. 41 00:02:34,030 --> 00:02:38,350 If you change the size of view, you want to use the request layout function. 42 00:02:38,350 --> 00:02:40,830 And if you change both the content of the view and 43 00:02:40,830 --> 00:02:45,910 the size, then you'll need to call both invalidate and request layout. 44 00:02:45,910 --> 00:02:50,060 So at the bottom of our original showLast function, let's add a call to invalidate. 45 00:02:51,580 --> 00:02:52,819 And if we run the app again. 46 00:02:54,952 --> 00:02:56,552 And then pick one of the options. 47 00:02:58,589 --> 00:02:59,660 Perfect! 48 00:02:59,660 --> 00:03:02,170 Instead of all the data, we've just got the past year. 49 00:03:02,170 --> 00:03:07,440 And if we click through the rest of these options, it looks like they all work. 50 00:03:07,440 --> 00:03:10,100 Though, since we aren't changing any of the y coordinates for 51 00:03:10,100 --> 00:03:14,460 something like past week, it pretty much just looks like a line. 52 00:03:14,460 --> 00:03:16,080 We'll fix that in the next video.