1 00:00:00,170 --> 00:00:04,460 We just programmed the heart of our app, the random number generator. 2 00:00:04,460 --> 00:00:08,180 Now we need to use that generator to help us pick a random fact 3 00:00:08,180 --> 00:00:09,940 on each button click. 4 00:00:09,940 --> 00:00:13,300 But before we can do that, we'll need a pool of facts to choose from. 5 00:00:14,790 --> 00:00:17,710 Let's start by creating an array to hold our facts. 6 00:00:17,710 --> 00:00:22,300 Remember, an array is just a container that holds values of a specific type. 7 00:00:22,300 --> 00:00:26,880 Let's add two lines to the top of our OnClickListener, and 8 00:00:26,880 --> 00:00:29,540 then create a new val named facts. 9 00:00:31,150 --> 00:00:32,950 Now, for the assignment side of things. 10 00:00:32,950 --> 00:00:35,940 For the sake of time, I'm going to paste in ten facts and 11 00:00:35,940 --> 00:00:37,830 then we'll go over the syntax. 12 00:00:37,830 --> 00:00:39,560 You can type these out if you want, but 13 00:00:39,560 --> 00:00:43,700 I've put this code in the teacher's notes so you can just copy and paste as well. 14 00:00:43,700 --> 00:00:49,460 Let's do it, I'll select everything after the word facts, then I'll 15 00:00:49,460 --> 00:00:53,620 copy this and paste it after our facts variable back in the OnClickListener. 16 00:00:56,576 --> 00:01:00,740 And now we can see that one way to initialize an array in Colin, 17 00:01:00,740 --> 00:01:05,000 is just to type arrayOf, and then add each item as a parameter. 18 00:01:07,240 --> 00:01:09,930 Now that we have an array of strings to select from, 19 00:01:09,930 --> 00:01:14,160 let's use our random number generator to help us pick one at random. 20 00:01:14,160 --> 00:01:17,730 Our array has ten elements in it, lets start 21 00:01:17,730 --> 00:01:22,494 by changing our random number generator to have a range of 10 instead of 3. 22 00:01:24,480 --> 00:01:29,760 If we clicked on our button now, we'd see random numbers ranging from 0 to 9. 23 00:01:29,760 --> 00:01:31,740 And what else do we have that arranges from 0 to 9? 24 00:01:33,240 --> 00:01:35,760 That's right, the indices of our facts array. 25 00:01:37,300 --> 00:01:40,930 This first fact about ants is at index 0, and 26 00:01:40,930 --> 00:01:45,440 this last fact about mammoths is at index 9. 27 00:01:45,440 --> 00:01:49,230 So if we want to select a random fact, we just need to select the fact 28 00:01:49,230 --> 00:01:53,390 from our facts array that's at the index of the random number we generated. 29 00:01:54,640 --> 00:01:55,385 Seems easy enough, 30 00:01:55,385 --> 00:01:58,860 let's start by getting rid of what we're currently setting our fact to. 31 00:02:01,910 --> 00:02:06,585 And then type, facts [randomNumber] and 32 00:02:06,585 --> 00:02:11,900 make sure you have a right bracket to close it off. 33 00:02:13,250 --> 00:02:16,520 This selects the fact at the index of our random number. 34 00:02:17,730 --> 00:02:19,215 All right, that should do it, 35 00:02:19,215 --> 00:02:23,200 but before we test our app, there's a potential bug we should fix first. 36 00:02:24,700 --> 00:02:27,760 What happens if we add a new fact to the array? 37 00:02:27,760 --> 00:02:29,950 Can you tell where the code will break? 38 00:02:29,950 --> 00:02:32,126 I'll add another fact and then let's talk about it. 39 00:02:32,126 --> 00:02:40,000 I'll say, Treehouse is not actually in a tree. 40 00:02:42,340 --> 00:02:47,010 Now that we have 11 elements in our array, we need to change 10 to 11 down here, 41 00:02:47,010 --> 00:02:49,790 and our call to nextInt. 42 00:02:49,790 --> 00:02:53,570 But if we forget to change it, we'll never see our new fact. 43 00:02:53,570 --> 00:02:57,020 Wouldn't it be nice if we could somehow use the number of elements in the array 44 00:02:57,020 --> 00:03:00,050 as our parameter, instead of a hard coded number like this? 45 00:03:01,080 --> 00:03:03,745 Then we wouldn't have to change it every time we add or 46 00:03:03,745 --> 00:03:05,080 remove elements in our array. 47 00:03:06,410 --> 00:03:09,510 Sure enough, Colin has a way for us to do this. 48 00:03:09,510 --> 00:03:15,560 Let's delete the number 10, and type facts.size. 49 00:03:15,560 --> 00:03:18,280 This gives us the size of the facts array, 50 00:03:18,280 --> 00:03:21,330 which is another way to say the total number of elements. 51 00:03:22,430 --> 00:03:24,480 By using this property of the array, 52 00:03:24,480 --> 00:03:27,590 we will always pass in the right number of elements. 53 00:03:27,590 --> 00:03:30,480 This saves us the work of manually changing numbers, and 54 00:03:30,480 --> 00:03:32,900 it also protects us from errors. 55 00:03:32,900 --> 00:03:37,350 For example, if we had deleted an element, I'll delete this one that I just added 56 00:03:39,990 --> 00:03:44,414 But we left this number as 11, then our app might try to reference 57 00:03:44,414 --> 00:03:47,970 an element that doesn't exist, and it would crash. 58 00:03:48,970 --> 00:03:53,230 Looks good to me, let's click on the run button to test our app. 59 00:03:56,055 --> 00:03:59,760 Then we can tap the button, and we should see some of the new facts. 60 00:04:01,300 --> 00:04:03,440 And there we go, we got something new. 61 00:04:03,440 --> 00:04:07,470 Olympic gold medals are actually made mostly of silver? 62 00:04:07,470 --> 00:04:10,812 And if we keep clicking, we should eventually see all of our facts, 63 00:04:10,812 --> 00:04:12,112 very cool! 64 00:04:14,446 --> 00:04:19,310 Awesome work, don't worry if all these new Android words aren't sticking yet. 65 00:04:19,310 --> 00:04:22,220 We've done a lot, and we still have a lot to do. 66 00:04:22,220 --> 00:04:25,570 But if you stick with it, I'll explain everything you need to know. 67 00:04:25,570 --> 00:04:28,180 And if you wanna talk about anything you're stuck on, 68 00:04:28,180 --> 00:04:31,320 head over to the community for questions or discussions. 69 00:04:31,320 --> 00:04:33,850 It's easy to feel overwhelmed with this stuff, but 70 00:04:33,850 --> 00:04:38,200 trust me, things will start to make more sense the more you practice. 71 00:04:38,200 --> 00:04:40,380 On that note, why don't you try some more practice?