Content Provider – Part 1 – Contract class

A Content Provider(CP) is a component that encapsulates data, expose them through a standard API and optionally make these data available to other applications. CP is independent of a data storage, which can be local, remote or mixed.

Contract class

We need a CP contract class to describe the data exposed from the CP(tables, columns, SQL creation strings, etc). The table information is in nested classes, a separate class for each table. Those classes should describe how to access the table, the table’s URI and the column name. The table URI has 3 parts:

content://me.kandz.gameimagedownloader.provides/tableName
  • content:// – scheme
  • me.kandz.gameimagedownloader – package name
  • tableName – the table name

To create a CP contract class, right-click the package, then click New and Java class. Specify the class name, mark it as final and click OK

Create a Content Provider contract class, <span>Content Provider – Part 1 – Contract class</span>, KandZ

Create a private constructor so no instance will be created:

private gamesCPcontract(){}

Create the authority string and the authority URI

Create a Content Provider contract class, <span>Content Provider – Part 1 – Contract class</span>, KandZ

Create nested class for each exposed table

Create a Content Provider contract class, <span>Content Provider – Part 1 – Contract class</span>, KandZ

Create a string for the path

Create a Content Provider contract class, <span>Content Provider – Part 1 – Contract class</span>, KandZ

Create an URI constant to access this table

Create a Content Provider contract class, <span>Content Provider – Part 1 – Contract class</span>, KandZ

Now it is time to add the column constant. It is a good practice to put the columns constants in protected interfaces and then implement the inner class, in case of identical columns across multiple tables. Also, you should put the common columns in one interface and remove it from the other interface. Create the protected interface and add the column constants.

Create a Content Provider contract class, <span>Content Provider – Part 1 – Contract class</span>, KandZ

Implement the interface, together with the baseColumn if you need the _ID column

Create a Content Provider contract class, <span>Content Provider – Part 1 – Contract class</span>, KandZ

THE END!!!

99250cookie-checkContent Provider – Part 1 – Contract classContent Provider – Part 1 – Contract class

Related posts