Free Android app covering all aspects of addiction, including prevention and treatment

Creating our Custom Content Provider: Part 2. Using the provider

  • Written by  Clive

Using the Content Provider

 

Using the content provider iconWe covered building our provider class in Part 1. Now in Part 2, we’ll see how we use the provider to access the database.

We will be accessing the database from within the app that created it but we will still use a content resolver to do so.

MainActivity.java

This is the activity that starts the app.

The MainActivity screenshot

A screenshot of the MainActivity

It has four buttons. These are used to demonstrate how to access the database using a content resolver.

Pressing a button, executes the relevant method.

Updating a row

Pressing the update button executes two methods:

  • insertRecord() – this inserts a row into the database. This makes sure that there is a row for us to update
  • updateRecord() – this updates the row that we inserted

We follow the following steps to update the row:

  • get the content resolver. We call getContentResolver() to do this
  • create a ContentValues  object and put the values that you want saved in the database into it. The content values object saves data as a key/value pair. Use the column title as the key
  • selection – this is the WHERE clause. We’re looking for a specific phone number so our where clause looks like this, PHONE = ?
  • selectionArguments – this is a string array of values to replace the selection wildcard (?). In our case it’s the phone number we’re looking for
  • cr.update() – this updates the row. This information is passed on to the content provider where the corresponding method is executed and the result returned. The content resolver method receives a number of parameters:
    • the CONTENT_URI, the Uri of the row to update
    • values – the database table column names and values to update
    • selection – the where clause to filter the specific phone number that we want to change
    • selectionArgument – the matching phone number for the where clause
    • numberRecordsUpdated - number of rows updated

Running the app and pressing the update button prints this in the LogCat:

The content provider updateLog

The update log

Note the following:

  • Both the insertRecord() and the updateRecord() methods include a Log call to print the result of calling getType()
  • insertRecord() logs the uri of the inserted row
  • updateRecord() logs the number of rows updated

Deleting all the rows of data

Pressing the delete button calls the deleteRecords() method.

We follow the following steps to delete the rows:

  • get the content resolver. We call getContentResolver() to do this
  • selection – we set the selection string to null as we want to delete all the rows in the database
  • selectionArguments – this is also set to null
  • cr.delete() – this deletes the rows. It calls the corresponding method in the content provider. The content resolver method receives a number of parameters:
    • the CONTENT_URI, the Uri of the rows to delete
    • selection – the WHERE clause. The filter to select specific rows. We want to delete all rows so it is set to null
    • selectionArguments – the values for the selection wildcards. It’s set to null as there are no wildcards
    • numberRowsDeleted = the number of rows deleted

Running the app and pressing the delete button prints this in the LogCat:

Content Provider deleteLog

The delete log

Note the following:

  • The first and second lines are after calling delete when the database was empty. It logs the data type and indicates that no rows were deleted
  • I then pressed the add button twice. This inserted two rows. There uri’s are shown here
  • I then pressed the delete button again. It logs the data type and indicates that two rows were deleted (they both had the same phone number)

Inserting a row of data

Pressing the insert button calls the insertRecord() method.

We follow the following steps to insert a row:

  • get the content resolver. We call getContentResolver() to do this
  • create a ContentValues  object and put the values that you want saved in the database into it. The content values object saves data as a key/value pair. Use the column title as the key
  • cr.insert() – inserts the row into the database by calling the corresponding content provider method. The method is passed a number of parameters:
    • the CONTENT_URI, the Uri of the table to insert into
    • values – these are the values that you want to insert into the database. They’re contained in the ContentValues object
    • insertedUri – this is the Uri of the inserted row
    • insertedRecordId – use getLastPathSegment() to get the row id of the inserted record. We use it later to demonstrate finding this specific row when we discuss the query() method

Running the app and pressing the insert button prints this in the LogCat:

The content provider insertLog

The insert log

Note the following:

  • The first line is the MIME type
  • The second line is the Uri of the inserted record

Get one row of data

Pressing the get record button calls the getRecord() method.

We follow the following steps to get a specific row of data:

  • We call insertRecord() to make sure that we have a record to find
  • recordId – we create this long object and assign it the ID of the record that we inserted. We’re going to look for and retrieve this specific row
  • get the content resolver. We call getContentResolver() to do this
  • we create a Uri for the specific record we’re looking for. We use ContentUris.withAppendedId() to do this. It builds a uri, appending the row ID to the CONTENT_URI
  • projection – a string array of the column titles that we want returned. Passing null will return all the columns
  • selection – a String - the WHERE clause. The filter to select specific rows. We want to select all rows so it is set to null
  • selectionArguments –a String[] -  the values for the selection wildcards. It’s set to null as there are no wildcards
  • sortOrder – a String – whether the returned results should be ordered (alphabetically for example). Passing null uses the default which could be unordered
  • cr.query() – calls the content provider’s corresponding method. It uses the uri of the row, looks for it and returns it’s data in a cursor
  • cursor – we loop through the cursor and display the individual column values in the log

Running the app and pressing the get button prints this in the LogCat:

The content provider getLog 

The get row log

Note the following:

  • the first line is the MIME type for the inserted row. It is of the dir type
  • the second line is the uri of the inserted row
  • we’re using the row ID of the inserted row in our query, it’s 173
  • note the MIME type for the uri of the row we’re looking for. It is of the item type
  • the last line shows the values for all the columns for the row with an id of 173

I hope that you have found this tutorial helpful.

You may also be interested in this tutorial on using Loaders to get the data out of the database.  

Please consider subscribing to our notification email. We’ll send you one email on Friday with links to our latest tutorials. That way you won’t miss out. If we didn’t publish any then we won’t send any email. No spam. 

This tutorial was created using Android Studio . You can download the project files here download35

Are you using Eclipse or another IDE? Here's how you can use this project's Android Studio files