Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Learning the App5:01 with Ben Deitch
In this video we'll look through the starter app to get a good feel for what we'll be replacing with a Content Provider!
In the last video, 0:00 I mentioned that we're going to start with an app that's already been built. 0:01 So pause me and take a second to download the app from the teacher's notes below and 0:04 open it up in Android Studio. 0:08 All right, now that we've got the app, 0:12 let's take a minute to get acquainted with what's going on here. 0:13 Let's start with the data side of things by jumping into the DatabaseHelper class. 0:17 And then I'll minimize the side bar to give us a little more room. 0:21 This class extends from SQLiteOpenHelper and is responsible for 0:25 managing our database. 0:29 It starts by declaring a few constants. 0:31 We've got the name of the database and the database version, followed by the name of 0:34 our table and the columns in that table, ID and name. 0:38 Then we've got a field to hold an instance of our database and then the constructor 0:44 where we call through the super and then populate our database field. 0:49 After that we've got the on create method 0:54 where we create the vics table using our ID and name columns. 0:56 Then we've got an upgrade which we aren't using, and then we've got the two methods 1:02 we are using to interact with our database, add contact and get contacts. 1:06 Add contact takes on a VICS name and then inserts that name into our VICS table. 1:12 On the other hand, the get contacts function queries the VICS table for 1:17 all of its contacts and returns an array of rows. 1:21 Let's click on the row and then use command or 1:25 control B to jump to the declaration. 1:27 And here we can see that row is just a simple class used to store one row of 1:31 the VICS table. 1:36 Just an ID and a name. 1:37 Okay let's close the row class and get back to the getContacts method. 1:41 We start by creating our select statement and 1:45 then executing it to get the results and to a cursor. 1:49 Then we create an array of rows. 1:53 And we populate that array by looping through our cursor. 1:56 Finally, we close the cursor and return our rows, cool. 2:00 Now that we've seen how the data layer works, let's switch gears and 2:05 take a look at the UI. 2:08 Jumping over to activity_main.xml. 2:11 Our layout starts with a vertical, and let me hide the preview for now. 2:17 Our layout starts with a vertical linear layout that has two children. 2:23 The first child is a horizontal linear layout with two children of it's own. 2:27 An edit text named nameEditText and a button named addButton. 2:33 The second child is a scroll view that contains another linear layout 2:39 named linearLayout. 2:43 And if we click on the design tab, 2:45 we can get a pretty good idea of what this will look like. 2:47 Awesome, moving on to main activity. 2:51 We start by declaring a field for a linear layout and another for a database helper. 2:55 Then inside onCreate, we set the layout, 3:00 Populate our dbHelper and then set up our view variables. 3:07 After that, we set an OnClick listener for our button. 3:12 And when our button is clicked, we're going to take the text from our edit text 3:15 if there is any and add it to our VICS table. 3:21 Then we'll update our list and set our edit text back to empty. 3:27 We'll also call update list at the end of the on create function 3:32 to handle initializing our list. 3:35 Inside the update list function, 3:38 we're going to start by removing all the views attached to our list. 3:41 Then, we're going to get an array of all the rows we need to show and for each row 3:45 we're going to get a new text view and add that text view to our linear layout. 3:50 Also just to be clear, if this was a production app, 3:56 you'd probably want to use a recycler view instead of a linear layout. 3:59 However, using a linear layout makes for a much simpler app and 4:03 should make it easier for us to focus on content providers. 4:07 So we'll be sticking with the linear layout. 4:10 All right, now that we've looked over the app and 4:13 have a good idea of how it should work, let's run it so we can see it in action. 4:16 And there we've got our edit text and our button, and 4:28 if we type in a name like Gertrude, and then click the button. 4:31 We see Gertrude on our list with an ID of one. 4:36 And if we type in another name like Charles and 4:39 then hit the button again, then we'll see him down below as well with an ID two. 4:43 Nice. 4:49 Not only do we have a good understanding of the app but the app also works. 4:50 In the next video, 4:55 we'll start looking at some next steps to start adding in a content provider. 4:56
You need to sign up for Treehouse in order to download course files.Sign up