In this codelab, You'll learn how to quickly add a ContentProvider to your project based on your model classes simply by annotating them using the SimpleSQLProvider library.

What you'll learn

What you'll need

First this is to setup your dependencies in Gradle.
In your top level project Gradle script , add the apt plugin classpath:

 
     buildscript {
           repositories {
               jcenter()
           }
           dependencies {
               classpath 'com.android.tools.build:gradle:1.3.0'
               classpath 'com.neenbedankt.gradle.plugins:android-apt:1.6'
           }
         }
        

Apply the apt plugin to your main project's build.gradle


           apply plugin: 'com.android.application'
           apply plugin: 'com.neenbedankt.android-apt'
          

Add the simple-sql-provider dependencies

           dependencies {
               compile 'com.squareup:javapoet:1.2.1'
               compile 'ckm.simple:simple_sql_provider_annotation:1.0.7'
               compile 'ckm.simple:simple_sql_provider_processor:1.0.7'
           }
          

You setup your provider configurations in one class as shown below. This class is used at compile time to generate all the boilerplate code for your custom Content Provider

@SimpleSQLConfig(
 name = "TodoProvider",
 authority = "mbanje.kurt.todo.AUTHORITY",
 database = "todo_database.db",
 version = 1)
public class TodoProviderConfig implements ProviderConfig {

    @Override
    public UpgradeScript[] getUpdateScripts() {
        return new UpgradeScript[0];
    }
}
           
          

This simply works by allowing you to annotate the class files you'd want to present as tables. In our example. we will create a table called "todo" and this will be accessible by making use of the TodoProvider we configured earlier.

@SimpleSQLTable(table = "todo",provider = "TodoProvider")
public class TodoItem {

    @SimpleSQLColumn(value = "_id",primary = true)
    public long _id;

    @SimpleSQLColumn("label")
    public String label;

    @SimpleSQLColumn("descr")
    public String description;

    @SimpleSQLColumn("completion_date")
    public boolean completed;
    }
       
     

Once you've annotated all the classes, rebuild your project and this will create all the necessary boilerplate classes for you.
You will now need to add the generated provider class to your Android manifest file & include the authority:


          <provider
            android:authorities="mbanje.kurt.todo.AUTHORITY"
            android:exported="false"
            android:name=".TodoProvider"/>
       

You will now have access to files generated for you to access the Table prefixed with "Table" using the usual Android ContentProvider methods .e.g.
TodoItemTable for the above class. The generated files have convinience functions for you to add values to the table, e.g. getContentValues() with an instance of the TodoItemTableclass to insert into db e.g.

  
TodoItem objectInstance = new TodoItem(...);
 getContentResolver().insert(TodoItemTable.CONTENT_URI,TodoItemTable.getContentValues(objectInstance,false));   
  

You can also get back data using the convinience getRowX methods:


Cursor cursor = getContentResolver().query(TodoItemTable.CONTENT_URI,
null,null,null,null);
//one row
TodoItem item = TodoItemTable.getRow(cursor,true);
//multiple rows
List items = TodoItemTable.getRows(cursor,false);   
         
       

I ported the Simple To-do list app I did way back to use this library. It uses most of the features of the library if you need some motivation.
Check the project on GitHub. If you have suggestions, create a pull request.