Udacity – Developing Android Apps
Lesson How to Use a Content Provider notes:
- ContentProvider presents data to external applications as one or more tables that are similar to the tables found in a relational database.
- Reasons to use ContentProvider
- Easily change underlying data source
- Leverage functionality of Android Classes.
- Allow many apps to access, use and modify a single data source securely.
- ContentProvider permission are in the AndroidManifest.xml
- e.g. android.permission.READ_USER_DICTIONARY for read only permission for user dictionary.
- An application accesses the data from a content provider with a ContentResolver client object. The ContentResolver methods provides query, insert, update, delete functions of persistent storage. ContentResolver calls these corresponding methods on the ContentProvider.
- Content URI is Uniform Resource Identifier that identifies data in a provider. Content URI include the symbolic name of the entire provider (authority) and a name that points to a table (path).
- e.g. User.Dictionary.Words.CONTENT = content://user_dictionary/words
- content:// is standard way Content URI starts (scheme)
- user_dictionary/ is Content Authority
- words mean to get list of words
- Cursor is an Iterator that give access to the underlying data in tabular form.
- getCount() return the number of rows in the cursor
- moveToNext() move the cursor to the next row
- get___(col index). e.g. getString, getInt…etc.
- getColumnIndex(name of column)
- call close() to avoid memory leak
- The Big Picture
- Your App request a ContentResolver and pass a URI into the resolver
- The resolver passes the information in the URI to the content providers. The resolver also calls the same method to the provider base on the method the app called on the resolver.
- Once Content provider gets the information from the database, it passes a cursor back to the resolver and subsequently pass the cursor back to the app.
- Cursor Adapter is like an array adapter but take a cursor to populate listview. (see SimpleCursorAdapter)
- android.R.layout.two_line_list_item is a standard layout
- use v4 SimpleCursorAdapter for compatibility