Ya.*_*man 28 android android-contentprovider
在最近更新的Android开发指南中,内容提供商的文档包含标题为合同类的部分.虽然有一个指向联系人示例的链接,但目前还不清楚什么是合同类,以及如何为我的自定义内容提供商创建一个
希望得到一些帮助.
谢谢!
Yog*_*ity 21
什么是合同类?
契约类是一个public final包含URI,列名,MIME类型和其他元数据的常量定义的类ContentProvider.它还可以包含static帮助方法来操作URI.
为什么用它?
怎么用?
以下是为包含两个表的天气应用程序设计的示例Contract类代码段:weather table和location table.跳过注释和一些方法以保持小.一般来说,应该评论很好.
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);
}
.
.
.
}
}
Run Code Online (Sandbox Code Playgroud)
evi*_*one 16
契约类定义了有助于应用程序使用内容URI,列名,意图操作和内容提供程序的其他功能的常量.合同类不会自动包含在提供者中; 提供者的开发人员必须定义它们,然后将其提供给其他开发人员.
您可以创建自己的Contract类并在那里定义一些常量.例如,以后可以在对数据库进行查询的代码中调用的列名等.
如何使用Contract类的好例子请参阅此线程Android - 如何加载联系人照片?
| 归档时间: |
|
| 查看次数: |
19737 次 |
| 最近记录: |