Lecture 7 Providers and Loaders

This lecture discusses how to access data from a Content Provider using a Loader. A Content Provider is an abstraction of a data base or other data store, allowing us easily systematically work with that data in Java (rather than in a separate data manipulation language such as SQL). A Loader is then used to efficiently perform this data access in the background (off the UI Thread), while also easily connecting that data to Views.

This lecture references code found at https://github.com/info448-s17/lecture07-loaders.

7.1 Content Providers

Consider the WordListFragment utilized by the example code (though these concepts apply to any Fragment or Activity). This Fragment includes a ListView that shows a list of words. Recall that a ListView utilizes the model-view-controller architecture… and in this case, the “model” (data) is a hard-coded list of array of words. But there are other lists of words as well! Entire databases of words! Previous lectures have discussed how to use network requests to access online data APIs, but there are also databases (of words no less) built into your Android phone.

For example, Android keeps track of the list of the spellings of “non-standard” words in what is called the User Dictionary. You can view this list on the device at Settings > Language & Input > Personal Dictionary. You can even use this Settings interface to add new words to the dictionary (e.g., “embiggen”, “cromulent”, “fleek”).

Note that the User Dictionary keeps track of a database of words. You can think of this database as being like a single SQL table: it’s a set of entries (rows) each of which have some values (columns). The primary key of the table is named (by convention) ID.

While you don’t need to know SQL to utilize a built-in database like the User Dictionary, it helps to have a passing familiarity with relational databases (e.g., what is taught in the iSchool’s INFO 340 course).

Since this data is stored in a (essentially) a simple SQL table, it is possible for us to access and modify it programmatically&mash;moreover, the Android framework allows us to do this without needing to know or write SQL! For example, we can access this list of words in order to show them in the WordFragment’s ListView.

  • To do this, we’ll need to request permission to access the database, just as we asked permission to access the Internet. Include the following in the Manifest:

    <uses-permission android:name="android.permission.READ_USER_DICTIONARY">

Although the words are stored in a database, we don’t know the exact format of this database (e.g., exact table or column names, or even whether it is an SQL database or just a .csv file!). We want to avoid having to write code that only works with a specific format, especially as the words may be stored in different kinds of databases on different devices or across different versions of Android. (The Android framework does include support for working directly with a local SQLite database, but it is a lot more work, requires knowing SQL, and produces a more fragile application).

In order to avoid relying on the specific format of how some data is stored, Android offers an abstraction in the form of a Content Provider. A Content Provider offers an interface to interact with structured data, whether that data is stored in a database, in a file, in multiple files, online, or somewhere else. You can thus think of “a Content Provider” as meaning “a data source” (e.g., the source/provider of content)!

  • It is possible to create your own Content Providers (described in a later lecture), but this lecture focuses purely on utilizing existing Providrs.

All Content Providers (data sources) have a URI (Universal Resource Identifier, a generalization of a URL used for resources not necessarily on the Internet). It is possible to query this URI, similar in concept to how web APIs are accessed via queries to their URI endpoints. In particular, Content Provider URIs utilize the content:// protocol (instead of https://), since the their data is accessed as via “content requests” rather than “HTTP requests”.

The URI for the Dictionary’s content is defined by the constant UserDictionary.Words.CONTENT_URI. We utilize constants to refer to URIs and paths to make it easier to refer to them and to generalize across devices that may have different directory structures.

We are able to access this Content Provider via a ContentResolver. This class provides methods for accessing the data in a provider (represented as a ContentProvider object). Each Context has a singleton ContentResolver, which is accessed via the getContentResolver() method (note that for a Fragment, the Context is the containing Activity). The ContentResolver’s methods support the basic CRUD operations: insert(), query(), update(), and delete().

ContentResolver methods take multiple parameters, supporting the different options available in a generic SQL query. For example, consider the the query() method:

getContentResolver().query(
    uri,              // The content URI
    projection,      // The an array of columns to return for each row
    selectionClause  // Selection criteria (as an SQL WHERE clause)
    selectionArgs,   // An array of values that can be injected into the selection clause
    sortOrder);      // The sort order for the returned rows (as an SQL ORDER BY clause)
  • This is basically a wrapper around an SQL SELECT statement!

The projection is a String[] of all the “columns” (attributes) we want to fetch from the data source. This is what you’d put after SELECT in SQL. (Note we can pass in null to represent SELECT *, but that’s inefficient—better to give a list of everything).

  • We can see what column names are available for the User Dictionary in UserDictionary.Words. Again, these are defined as constants!

  • Be sure to always select the _ID primary key: it will be needed later!

The other parameters can be used to customize the SELECT statement. THe “selection” (WHERE) clause needs to parameters: the second are values that will be escaped against SQL injection attacks. Passing null for any of these parameters will cause the clause to be ignored:

ContentResolver resolver = getActivity().getContentResolver();
String[] projection = new String[] { UserDictionary.Words.WORD, UserDictionary.Words._ID };
resolver.query(UserDictionary.Words.CONTENT_URI, projection, null, null, null);

So overall, the query is breaking apart the components SQL SELECT statement into different pieces as parameters to a method, so you don’t quite have to write the selection yourself. Moreover, this method abstracts the specific query language, allowing the same queries to be used on different formats of database (SQLite, PostgreSQL, files, etc).

7.2 Cursors

The ContentResolver#query() method returns a Cursor. A Cursor provides an interface to the list of records in a database (e.g., those returned by the query). A Cursor also behaves like an Iterator in Java: it keeps track of which record is currently being accessed (e.g., what the i would be in a for loop). You can think of it as a “pointer” to a particular record, like the cursor on a screen.

We call methods on the Cursor to specify which record we want it to “point” to, as well as to fetch values from the record object at that spot in the list. For example:

cursor.moveToFirst(); //move to the first item
String field0 = cursor.getString(0); //get the first field (column you specified) as a String
String name = cursor.getString(cursor.getColumnIndexOrThrow("word")); //get the "word" field as a String
cursor.moveToNext(); //go to the next item

The nice thing about Cursors though is that they can easily be fed into AdapterViews by using a CursorAdapter (as opposed to the ArrayAdapter we’ve used previously). The SimpleCursorAdapter is a concrete implementation that is almost as easy to use as an ArrayAdapter:

You instantiate a new SimpleCursorAdapter, passing it:

  1. A Context for loading resources
  2. A layout resource to inflate for each record
  3. A Cursor (which can be null)
  4. An array of column names to fetch from each entry in the Cursor (the projection, similar to before)
  5. A matching list of View resource ids (which should all be TextViews) to assign each column’s value to. This is the “mapping” that the Adapter will perform (from projection columns to TextView contents).
  6. Any additional option flags (0 means no flags, and is the correct option for us).

Then we can use this adapter for the ListView in place of the ArrayAdapter!

7.3 Loaders

In order to get the Cursor to pass into the adapter, we need to .query() the database. But we’ll be doing this a lot, and so would like to do it off the UI Thread—database accessing is slow! And every time we do that query (or any other database manipulation), we want to update the Adapter so that the changes to the list show up.

In order to easily update your list with new data loaded on a background thread, we’re going to use a class called a Loader. This is basically a wrapper around ASyncTask, but one that lets you execute a backgroup task repeatedly whenever the data source changes. In particular, Android provides a CursorLoader specifically used to load data from ContentProviders through Cursors—whenever the content changes, a new Cursor is produced which can be “swapped” into the adapter.

To use a CursorLoader, we need to specify that our Fragment implements the LoaderManager.LoaderCallback<Cursor> interface—basically saying that this fragment can react to Loader events.

  • Loaders need to work with Fragments, unless the Activity subclasses FragmentActivity (as AppCompatActivity does) and thereby provides the “Fragment” capabilities needed to use a Loader. So we can use Loaders in our Activities or Fragments.

We will need to fill in the interfaces callbacks functions in order to use the CursorLoader:

  • In onCreateLoader() we specify what the Loader should do. Here we would instantiate and return a new CursorLoader(...) that queries the ContentProvider. This looks a lot like the .query() method we wrote earlier, but will run on a background thread!

  • In the onLoadFinished() callback, we can swap() the Cursor into our SimpleCursorAdapter in order to feed that model data into our controller (for display in the view). See the guide for more details.

  • In the onLoaderReset() callback just swap in null for our Cursor, since there now is no content to show (the loaded data has been “reset”).

Finally, in order to actually start our background activity, we’ll use the getLoaderManager().initLoader(...) method. This is similar in flavor to the AsyncTask.execute() method we’ve used before (using a manager similar to the FragmentManager).

getLoaderManager().initLoader(0, null, this);

The first parameter to the initLoader() method is an id number for which cursor you want to load, and is passed in as the first param to onCreateLoader() (or is accessible via Loader#getId()). This allows you to have multiple Loaders using the same callback function (e.g., a Fragment can handle multiple Loaders for multiple data sources). The second param is a Bundle of args, and the third is the LoaderCallbacks (e.g., who handles the results)!

  • Note that you can use the .restartLoader() method to “recreate” the CursorLoader (without losing other references), such as if you want to change the arguments passed to it.

And with that, we can fetch the words from our database on a background thread—and if we update the words it will automatically change!

7.4 Other Provider Actions

7.4.1 Adding Words

To insert a new Word into the ContentProvider, we just call a different method on the ContentResolver:

//Example from Google:
ContentValues mNewValues = new ContentValues();
mNewValues.put(UserDictionary.Words.APP_ID, "edu.uw.loaderdemo");
mNewValues.put(UserDictionary.Words.LOCALE, "en_US");
mNewValues.put(UserDictionary.Words.WORD, word);
mNewValues.put(UserDictionary.Words.FREQUENCY, "100");

Uri mNewUri = getContentResolver().insert(
        UserDictionary.Words.CONTENT_URI,   // the user dictionary content URI
        mNewValues                          // the values to insert
);
  • Note that we specify the “details” of the Word in a ContentValues object, which is a HashMap almost exactly like a Bundle (but only supports values that work with ContentProviders)