An Idiot’s guide to Android Content Providers Part 2

sanjeev yadav
3 min readNov 21, 2017

--

We will be continuing from last where we left of, checkout Part1 if you haven’t seen

  1. We are going to create a class that will extend ContentProvider which will serve all Todo’s data and allow us to perform CURD operations.
//TODO: (1) Create a class TodoProvider that extends ContentProvider
//TODO: (2) Override onCreate, query, getType, insert, delete and update method.
//TODO: (3) Create TodoDbHelper variable in class
//TODO: (4) Inside onCreate, initialize the variable and return true

2. If you recall, we have two Uri which are -

content://com.example.todo/todo   #For accessing all records
content://com.example.todo/todo/2 #For accessing specific record

Now we need to create a UriMatcher to assign these two Uri unique integers that will help us to perform correct operation either on bulk or on individual record

//TODO: (1) Create two constants CODE_TODO = 100 and CODE_TODO_WITH_ID = 101
//TODO: (2) Create a UriMatcher variable in class
//TODO: (3) Create buildUriMatcher function that will return UriMatcher
//TODO: (4) Inside buildUriMatcher assign the uri with their unique integer code

3. All we need to do is implement all CURD Operations. Let’s start with insert

//TODO: (1) Get an instance of writable SQLiteDatabase from mOpenHelper
//TODO: (2) Use sUriMatcher.match function to match the uri
//TODO: (3) Insert data using SQLiteDatabase and store the row id
//TODO: (4) return the new Uri using buildTodoUriWithId function

Whenever we have to insert a new record we can simply call this function in MainActivity with task to do.

Uri insertNewRecord(String task){
ContentValues values = new ContentValues();
values.put(TodoContract.TodoEntry.COLUMN_TASK,task);

/* 0 stands for task hasn't been completed,
* 1 means task has been completed
*/
values.put(TodoContract.TodoEntry.COLUMN_STATUS,0);
values.put(TodoContract.TodoEntry.COLUMN_TASK,task);
values.put(TodoContract.TodoEntry.COLUMN_DATE,
System.currentTimeMillis());

return getContentResolver().
insert(TodoContract.TodoEntry.CONTENT_URI,values);
}

4. Query operation can be of two type, first to query all the records and second to query a single record with its row ID

//TODO: (1) Get an instance of readable SQLiteDatabase from mOpenHelper
//TODO: (2) Use sUriMatcher.match function to match the uri
//TODO: (3) If sUriMatcher return CODE_TODO_WITH_ID, retrieve the id from the uri using getLastPathSegment and query single record
//TODO: (4) else query the entire todo table for all records

Now we can use ContentResolver to query all records or single record in MainActivity and display data accordingly.

/* To query specific record */
long _id = 2; //Suppose we want to second row in the table
Cursor cursor = getContentResolver()
.query(TodoContract.TodoEntry.buildTodoUriWithId(_id),null,null,null,null);
/* To query all records */
Cursor cursor = getContentResolver()
.query(TodoContract.TodoEntry.CONTENT_URI,null,null,null,null);

Delete and update function can be implemented in similar manner. To learn more about Content Provider checkout

5. Finally and the most crucial part of it all, is to register your ContentProvider in AndroidManifest.xml

<!-- Our ContentProvider -->
<provider
android:name="TodoProvider"
android:authorities="com.example.todo"
android:exported="true" />

or try other articles from Idiot’s guide

If you enjoyed this article, please click the 👏 button and share to help others find it! Feel free to leave a comment below.

--

--

sanjeev yadav

Front end & Android Developer, in deep love with learning about CS and a travel enthusiast :) https://github.com/alexakasanjeev