【问题标题】:What is a Contract Class and how is it used什么是合同类以及如何使用它
【发布时间】:2012-03-03 20:12:29
【问题描述】:

在最近更新的 Android 开发指南中,内容提供程序的文档包含一个标题为 Contract Classes 的部分。虽然有一个联系人示例的链接,但目前尚不清楚什么是合同类以及如何为我的自定义内容提供程序创建一个

不胜感激。

谢谢!

【问题讨论】:

标签: java android android-contentprovider


【解决方案1】:

什么是合同类?

契约类是一个 publicfinal 类,它包含 URI、列名、MIME 类型和其他关于 ContentProvider 的元数据的常量定义。它还可以包含 static 辅助方法来操作 URI。

为什么使用它?

  1. 合同类在内容之间建立合同 提供者和其他应用程序。它确保您的 内容提供者即使有变化也能正确访问 到 URI、列名等的实际值。
  2. 由于它为其常量提供了助记名称,因此开发人员可以 不太可能对列名或 URI 使用不正确的值。
  3. 很容易使 Javadoc 文档可供客户端使用 想使用您的内容提供商。

如何使用?

这是一个为包含两个表的天气应用程序设计的合同类 sn-p 示例:天气表和位置表。 cmets 和一些方法被跳过以保持它的小。一般来说,它应该被很好地评论。

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);
        }

        .
        .
        .
    }
}

【讨论】:

    【解决方案2】:

    合约类定义了有助于应用程序使用的常量 内容 URI、列名、意图操作和其他功能 内容提供者。合同类不会自动包含在内 与提供者;提供者的开发者必须定义它们,然后 将它们提供给其他开发者。

    您可以创建自己的 Contract 类并在其中定义一些常量。例如,您可以稍后在查询数据库的代码中调用的列名等。

    如何使用 Contract 类的好例子,请参阅此线程 Android - How do I load a contact Photo?

    【讨论】:

      【解决方案3】:

      Contract 是一个常量容器,用于定义 URI 表和列的名称。它还为同一包中的所有其他类提供相同的常量。

      【讨论】:

        【解决方案4】:

        Contract 类是一个简单的类,它保存要在 SQL Lite DB 中创建的表的常量。我们可以根据所需表的数量添加内部类。使契约类final,使表的常量不能改变。

        合约类充当表类的容器,无需进一步修改

        表类(内部类)可能实现也可能不实现BaseColumns

        希望这可以清楚地了解合同类别! :-)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-01-10
          • 2015-03-13
          • 2013-10-25
          • 2014-11-19
          • 1970-01-01
          • 2011-11-01
          相关资源
          最近更新 更多