Inserting a New Row5:32 with Ben Deitch
In this video we'll see how to insert a new row into our database by using our ContentProvider!
We've just got one more function until we're done with our content provider and 0:00 that's the insert function, where we, unsurprisingly, 0:04 insert data into to our database. 0:08 To do that, let's add a line at the top and then call database.insert. 0:10 Then let's pass in our table DatabaseHelper.TABLE_VICS 0:17 followed by null and then finally our content values parameter 0:22 which just represents the data we'd like to insert. 0:27 Now while this does insert the row, we're not done yet. 0:33 Let's use F1 on Mac, or Ctrl+Q on Windows to look at the documentation for 0:37 our content providers insert method. 0:42 And then if we follow the link to the actual insert method. 0:52 And then read through this. 0:59 It looks like we should be calling some notifyChange method 1:01 after we finished inserting. 1:05 Also, if we scroll to the bottom, instead of returning null, 1:07 we should be returning the URI for the newly inserted item. 1:12 Back in the code, let's start by creating a new long named rowId and 1:16 setting it equal to the result of our insert statement. 1:21 So long rowId equals our insert statement. 1:25 The insert statement will return the rowId of the new row or 1:31 negative one if there was an error. 1:36 On the next line, let's make sure that we didn't get an error, 1:38 by checking that rowId is greater than -1. 1:42 Then, inside the if statement, let's create a new URI for the inserted row. 1:47 Uri, and let's name it newUri, and 1:53 then set it equal to ContentUris.withAppended Id. 1:58 And next, we need to pass in a content URI and our row ID. 2:06 For the content URI, let's pass in a constant that doesn't exist yet, 2:11 CONTENT_URI. 2:15 Then let's pass in our rowId and add a semi colon. 2:19 And then let's use ALT enter to create a constant. 2:25 You might have to hit escape to tell it you're not trying to import anything. 2:30 Then let's get rid of the private keyword because we'll need to access this from 2:37 elsewhere later. 2:40 And let's set this equal to Uri.parse, and 2:44 then we've just got to pass in our content URI as a string. 2:48 A content URI has three main parts. 2:55 The first part is the schema, and it's always going to be content://. 2:58 Next is the authority, which is typically just your package name plus provider. 3:06 So let's add com.teamtreehouse.contentproviders.provi- 3:12 der. 3:21 And finally, the third piece is the path 3:22 which specifies which table we're using and can also specify a specific row. 3:26 To add the path, let's just add a forward slash and 3:31 then add in our table which will be outside of the string, 3:35 +DatabaseHelper.TABLE_VICS. 3:40 Nice. 3:44 Now that we've got our content URI, back in the insert method, 3:45 we still need to call notify changed and return our new URI. 3:50 And to do that, we'll need to make use of a new object called a content resolver. 3:54 Content resolvers are just the way we communicate with our content providers. 3:59 If we're in another app, we won't have access to our content provider directly, 4:04 so instead we go through a content resolver and 4:09 just provide the URI for our content provider. 4:11 So below where we get our new URI, let's type 4:16 context.getContentResolver().notifyChange and 4:19 then let's pass in our new URI for the URI, and null for the observer. 4:28 Even though we won't be using any content observers in this workshop, 4:37 it's still a best practice to notify our changes, just in case. 4:40 Finally, on the next line, 4:44 let's return our new URI, return newUri. 4:47 And then at the end, let's throw an exception, instead of returning null. 4:52 So throw_new SQLiteException. 4:58 And for the message, let's type Insert 5:05 failed for Uri. 5:10 And we'll tell it the Uri. 5:15 Great work. 5:21 You've just finished your first content provider. 5:22 Coming up, we'll see how we can interact with our content provider by 5:25 updating main activity to use it instead of our database. 5:28
You need to sign up for Treehouse in order to download course files.Sign up