Loading Additional Pages2:40 with Ben Jakuben
Now we need to refactor our code to load different pages from our Story class. We also need to add OnClickListeners to our buttons to trigger the loadPage() method.
[MUSIC] 0:00 We left off with a simple load page method that gets called when our activity 0:04 is first created. 0:08 And as of right now, simply loads the first page of the story, 0:09 which is at page zero. 0:12 Now we want to change this to load a new page of the story based on whatever 0:13 the user taps on as one of the choices. 0:16 So, to do that, we need to add unclick listeners to our buttons and 0:18 call this loadPage method when a button is tapped. 0:21 That will cause all these views to be repopulated with fresh page data, 0:24 were ever leaving the activity. 0:27 We're just updating all the views inside it. 0:29 So let's add the new OnClickListeners down here where we're setting the text 0:32 for the buttons. 0:35 I'm going to keep the choice one code together so, 0:36 I'll add a few lines between here and then type choice1Button.setOnClickListener 0:39 new OnClickListener, hit Enter and there we go, we have the onClick method. 0:44 In here, the first thing we need is the index of the new page we want to load. 0:50 So int nextPage =, and then from the page story we can getChoice1 0:53 because we're in Choice1 button and get the NextPage. 0:58 Okay, I was actually expecting to see an error here about page not being final. 1:05 It looks like Android Studio might have automatically added the final keyword to 1:10 our page variable to make sure that we can access it down here in this anonymous 1:14 inner class. 1:18 We have to remember that this anonymous inner class this onClickListener is 1:19 a self-contained unit and 1:22 it can't reference anything from outside of it unless we make that kind of change. 1:24 All right, so now we can simply load this nextPage by calling, loadPage and 1:28 passing in nextPage. 1:32 Now we wanna do the exact same thing for choice two. 1:35 Let's cheat and do a quick copy paste. 1:38 As usual, I want to warn you to be very careful when copying and pasting code. 1:40 It's very easy to make a copy and paste errors. 1:44 Okay, so we want to change this to Choice2Button as well and 1:48 then we want to change down here instead of getting from Choice1, 1:51 let's get the next page from Choice2. 1:55 Okay, we should be set. 1:58 Let's run this to make sure that it's working as we expect it to. 1:59 All right, I'm going to go back to the Pixel, Use that for future launches and 2:02 click OK. 2:07 Okay, so this time, 2:09 I'm going to type in a different name, let's use Pasan, START YOUR ADVENTURE. 2:09 And once again, we see the name being plugged in. 2:14 And now, let's try the first choice, STOP AND INVESTIGATE. 2:16 Okay, cool, look at that, we went to a new page. 2:20 And, let's try the second choice too, EXPLORE THE ROVER. 2:23 All right, look at this. 2:27 We're working through the story. 2:28 Let's EXPLORE THE COORDINATES. 2:29 Uh-oh, we've run into an error. 2:30 All right, so what's going on here? 2:33 Here's a challenge. 2:35 See if you can figure out what the error is, and 2:36 then, I'll show you how we're going to fix it in the next video. 2:38
You need to sign up for Treehouse in order to download course files.Sign up