在最近更新的Android開發者指南中,內容提供者的文檔包含標題爲Contract Classes的部分。儘管聯繫人有一個示例鏈接,但尚不清楚什麼是合同類,以及如何爲我的自定義內容提供商創建一個合同類什麼是合同類以及它是如何使用的
希望對此有所幫助。
謝謝!
在最近更新的Android開發者指南中,內容提供者的文檔包含標題爲Contract Classes的部分。儘管聯繫人有一個示例鏈接,但尚不清楚什麼是合同類,以及如何爲我的自定義內容提供商創建一個合同類什麼是合同類以及它是如何使用的
希望對此有所幫助。
謝謝!
合同類定義幫助應用程序與 內容的URI,列名,意圖的行動,以及 其他功能內容提供商合作的常數。合同類別不會自動包含在供應商 中;供應商的開發人員必須定義它們,然後 使其可供其他開發人員使用。
您可以製作自己的Contract類並在其中定義一些常量。例如,列名,你可以在代碼中,使得數據庫查詢後調用等
很好的例子怎樣合同類用於看到這個線程Android - How do I load a contact Photo?
Contract
是定義名稱URI的常量容器表和列。它還在同一個包中的所有其他類中提供相同的常量。
什麼是合同類?
合同類是包含了URI的常量定義,列名,MIME類型和其他元數據有關ContentProvider
一個public
final
類。它也可以包含static
輔助方法來操作URI。
爲什麼使用?
它是如何使用的?
下面是一個示例合同類代碼片段,它爲包含兩個表格的天氣應用程序設計:天氣表和位置表。跳過評論和一些方法來保持它很小。一般來說,它應該很好地評論。
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);
}
.
.
.
}
}
另請參閱http://stackoverflow.com/questions/17451931/how-to-use-a-contract-class-in-android – OneWorld 2016-07-21 09:13:23