Content Provider – Part 2 – create and query

You can read part 1 here.

To create a Content Provider you need to:

  • extend the ContentProvider class
  • implement the lifecycle methods
  • implement the data lookup methods
  • implement the modification methods
  • create a Content Provider identification
  • specify Content Provider’s visibility to other apps

Content Provider is responsible to connect to the database and perform all the database interactions. The methods are similar to the SQLite API.

Add a Content Provider to your app

Right-click the package name, then click New, Other and then Content Provider. You will see the following window:

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

Specify the class name and the URI authorities which is the app package name and “.provider” at the end. Select Exported if you want the Content Provider to be accessed from other apps, and Enabled if you want Android to start it if other app using it. Click Finish

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

The new class file created, and added to AndroidManifest.xml a provider element.

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

Content Provide must connect to the SQLite Database, using an SQLiteOpenHelper class. Read more here how to implement this class. Create a member field:

private GameDBHelper mGameDBHelper;

Instantiate the above inside onCreate() method and return true so it will show that the Content Provider was created successfully.

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

Implement URI handling

Instantiate a UriMatcher class:

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

Now create a static initialiser and add a list with all the valid URIs for tables or single rows

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ
  • 1st – the authority
  • 2nd – the table path
  • 3rd – an int code unique for this URI. You can decide the number.

Set the cursor on the 3rd argument and press Alt + Ctrl + C to create a constant. Type the name and press Enter.

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

If you want to add and URI for single records, ID columns for this table, then you need to add almost the sane URI. You just need to add the # wildcard after the path.

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

query

Now, it is time to implement the query method. Create a Cursor variable and get reference to the readable db:

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

Use the match method from UriMatcher class to return the code of the uri.

int uriMatch = sUriMatcher.match(uri);

use a switch statement to the check the code the match method return and execute the appropiate query:

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

Now, to use the query from the activity, we need to use context’s getContentResolver() method, which return a cursor.

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

query a row

add a constant for the table row

public static final int GAMES_TABLE_ROW = 10;

add the URI for the query row

sUriMatcher.addURI(GamesCPcontract.AUTHORITY, GamesCPcontract.Games.PATH + "/#", GAMES_TABLE_ROW);

add support in the query switch for this URI

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

extract the id using the ContentUris class and its method parseid

long rowId = ContentUris.parseId(uri);

create a selection string, and selection string Array:

String columnSelection = GamesContract.GameEntry._ID + " = ? ";
String[] columnSelectionArgs = {Long.toString(rowId)};

execute the query

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

Now, to use the query from the activity, we need to use context’s getContentResolver() method, which return a cursor. First create the Uri for the specific row, using withAppendedId method. Then create a string array for the column you want to select. Last use getContentResolver()

create a content provider and query, <span>Content Provider – Part 2 – create and query</span>, KandZ

THE END!!!

99510cookie-checkContent Provider – Part 2 – create and queryContent Provider – Part 2 – create and query

Related posts