About
A contract describes how the information of the content provider is stored.
- For a table, it defines table and column names for the database.
- For an image, it define the file
- …
Articles Related
Example
Table definition
It defines:
- The Content authority. The unique name to identify the content provider. A convenient string to use for the content authority is the package name for the app, which is guaranteed to be unique on the device.
public static final String CONTENT_AUTHORITY = "com.example.android.sunshine.app";
- The Base Content Uri. The base is used to create the base of all URI's which apps will use to contact the content provider.
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
- Each columns of a row of a table as a static field in a subclass that implements BaseColumns (_ID and _COUNT are then automatically included). This static field can be used after in the UI or in an android helper class such as the sqlite SQLiteOpenHelper. See WeatherDbHelper.java
// Data path (appended to base content URI)
public static final String DATA_PATH = "aliasToTableData"; // Generally just the table Name
// For instance, content://com.example.android.sunshine.app/aliasToTableData/ is a valid path for
/* Inner class that defines the table contents of the location table */
public static final class TableEntry implements BaseColumns {
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(DATA_PATH).build();
// Content Type: Return more than one entry (a cursor or a dir)
// The content type is returned by the getType(Uri) function of the content provider
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + DATA_PATH;
// Content Type: Return only one row
// The content type is returned by the getType(Uri) function of the content provider
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + DATA_PATH;
// Table name
public static final String TABLE_NAME = "tableName";
// The columns name
public static final String COLUMN_LOCATION_SETTING = "location_setting";
public static final String COLUMN_CITY_NAME = "city_name";
public static final String COLUMN_COORD_LAT = "coord_lat";
public static final String COLUMN_COORD_LONG = "coord_long";
// The URI to return a row by Id
public static Uri buildLocationUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
Example
- The Contacts contract in Android
- The WeatherContract of the Sunshine app. Each entry of a table is a subclass that implements BaseColumns