我在安卓学习sqlite。对于sqlite,我正在引用developer.android.com。但我在读代码时有些困惑。他们写了构造函数来防止
实例化FeedReaderContract类,但它们没有在任何地方定义FeedReaderContract类以及FeedReaderContractFeedReaderContract之间的关系。
我指的是。我提供代码。如何在OpenHelper类中定义内部类。有人能给我提个好办法吗?.
例如,此代码段定义单个表的表名和列名:

public static abstract class FeedEntry implements BaseColumns
{
    public static final String TABLE_NAME = "entry";
    public static final String COLUMN_NAME_ENTRY_ID = "entryid";
    public static final String COLUMN_NAME_TITLE = "title";
    public static final String COLUMN_NAME_SUBTITLE = "subtitle";
    ...
}

为了防止有人意外地实例化契约类,请给它一个空的构造函数。
 //Prevents the FeedReaderContract class from being instantiated.
private FeedReaderContract() {}

最佳答案

我理解为:

public static class FeedReaderContract{

    // Prevents the FeedReaderContract class from being instantiated.
    private FeedReaderContract() {}

    //The FeedEntry table definition
    public static abstract class FeedEntry implements BaseColumns {
        public static final String TABLE_NAME = "entry";
        public static final String COLUMN_NAME_ENTRY_ID = "entryid";
        public static final String COLUMN_NAME_TITLE = "title";
        public static final String COLUMN_NAME_SUBTITLE = "subtitle";
        ...
    }

    //more tables definition
}

因此,您不能实例化契约,但可以访问所有内部类常量。如示例行:
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" +
FeedReaderContract.FeedEntry._ID + " INTEGER PRIMARY KEY," //continues

访问FeedEntry类中内部类的TABLE_NAME常量(_IDFeedReaderContract)。
希望有帮助。

09-15 22:08