¿Qué es una clase Contract?
Una clase contrato es una clase public
final
que contiene definiciones de constantes para la URIs, nombres de columna, tipos MIME, y otra de meta-datos acerca de la ContentProvider
. También puede contener static
métodos auxiliares para manipular los URI.
¿Por qué se usa?
- La Clase contrato establece un contrato entre el proveedor de contenido y otras aplicaciones. Se asegura de que su proveedor de contenidos se puede acceder correctamente, incluso si hay cambios a los valores reales de los URI, nombres de columna, etc.
- Dado que proporciona nombres mnemotécnicos por sus constantes, los desarrolladores son menos propensos a utilizar valores incorrectos para nombres de columnas o URI.
- Es fácil hacer que la documentación de Javadoc esté disponible para los clientes que desean usar su proveedor de contenido.
¿Cómo se utiliza?
Aquí hay un ejemplo de fragmento de clase de contrato diseñado para una aplicación meteorológica que contiene dos tablas: tabla meteorológica y tabla de ubicación. Los comentarios y algunos métodos se omiten para mantenerlo pequeño. En general, debe ser bien comentado.
public class WeatherContract {
public static final String CONTENT_AUTHORITY =
"com.example.android.sunshine.app";
public static final Uri BASE_CONTENT_URI =
Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_WEATHER = "weather";
public static final String PATH_LOCATION = "location";
/**Inner class that defines the table contents of the location table. */
public static final class LocationEntry implements BaseColumns {
public static final String TABLE_NAME = "location";
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";
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build();
// Custom MIME types
public static final String CONTENT_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE +
"/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION;
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE +
"/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION;
// Helper method
public static Uri buildLocationUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
}
/** Inner class that defines the table contents of the weather table. */
public static final class WeatherEntry implements BaseColumns {
public static final String TABLE_NAME = "weather";
public static final String COLUMN_LOC_KEY = "location_id";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_WEATHER_ID = "weather_id";
public static final String COLUMN_SHORT_DESC = "short_desc";
public static final String COLUMN_MIN_TEMP = "min";
public static final String COLUMN_MAX_TEMP = "max";
public static final String COLUMN_HUMIDITY = "humidity";
public static final String COLUMN_PRESSURE = "pressure";
public static final String COLUMN_WIND_SPEED = "wind";
public static final String COLUMN_DEGREES = "degrees";
public static final Uri CONTENT_URI =
BASE_CONTENT_URI.buildUpon().appendPath(PATH_WEATHER).build();
public static final String CONTENT_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY +
"/" + PATH_WEATHER;
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY +
"/" + PATH_WEATHER;
// Helper method.
public static Uri buildWeatherUri(long id) {
return ContentUris.withAppendedId(CONTENT_URI, id);
}
.
.
.
}
}
Vea también http://stackoverflow.com/questions/17451931/how-to-use-a-contract-class-in-android – OneWorld