Introduction to Android Content Providers
A Content Provider is used to share and access to a structured set of data. The main idea is to encapsulate the data that we want to share and provide mechanisms for access this data safely through a ContentResolver interface, first the client make a request to the Content Resolver, next the Content Resolver communicates with the Content Provider that is responsable of performs the requested action and returns the results.
Methods to use in a Content Provider
- query(Uri, String[], String, String[], String) which returns data to the client request
- insert(Uri, ContentValues) which inserts new data into the content provider
- update(Uri, ContentValues, String, String[]) which updates existing data in the content provider
- delete(Uri, String, String[]) which deletes data from the content provider
- getType(Uri) which returns the MIME type of data in the content provider
Content URIs
A Content URIs identifies the structured set of data in a provider. The Content URIs have the syntax content://authority/path/id
- content: Is the scheme portion of the URI and always looks like “content://”
- authority: Content Provider identifier. All the content URIs for the provider must start with this field . To guarantee a unique authority it is advisable to use the same as the provider class package identifier.
- path: Optional segments separated by a forward slash (/) that identify some subset of the provider’s data for example this is used to identify some individual data tables.
- id: A unique numeric identifier for a single row in the subset of data.
Simple example
The Android User Dictionary is a Content Provider of user defined words for input methods to use for predictive text input, for this example we will use the User Dictionary to show a list of words and the frequency of use. The Content URI to use must be “content://user_dictionary/words”.
MainActivity.java
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.UserDictionary;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;public class MainActivity extends ActionBarActivity {// For the SimpleCursorAdapter to match the UserDictionary columns to layout items.
private static final String[] COLUMNS_TO_BE_BOUND = new String[] {
UserDictionary.Words.WORD,
UserDictionary.Words.FREQUENCY
};private static final int[] LAYOUT_ITEMS_TO_FILL = new int[] {
android.R.id.text1,
android.R.id.text2
};@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// Get the TextView which will be populated with the Dictionary ContentProvider data.
ListView dictListView = (ListView) findViewById(R.id.dictionary_list_view);// Get the ContentResolver which will send a message to the ContentProvider
ContentResolver resolver = getContentResolver();// Get a Cursor containing all of the rows in the Words table
Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);// Set the Adapter to fill the standard two_line_list_item layout with data from the Cursor.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.two_line_list_item,
cursor,
COLUMNS_TO_BE_BOUND,
LAYOUT_ITEMS_TO_FILL,
0);// Attach the adapter to the ListView.
dictListView.setAdapter(adapter);
}
}
activity_main.xml<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"><ListView
android:id="@+id/dictionary_list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></RelativeLayout>
And finally the app should looks like: