Fran M.
Fran M.

Reputation: 79

Accessing class private members from another class

I'm new to programming, and I'm starting with Java and Android. I want to create a database, so I've followed the offical documentation: https://developer.android.com/training/basics/data-storage/databases.html

My problem comes when, on the Contract class, the members to create and maintain the database are private strings (SQL_CREATE_ENTRIES and SQL_DELETE_ENTRIES). Defining the DBHelper class on another file, I just cannot access that members the way it appears on the documentation (the FeedReaderDbHelper class just use them as if they were inside it scope). So Android Studio just put the sentence on red and says: 'Cannot resolve symbol'.

What should I do here? I've read as well on the documentation that setters/getters are not recommended for Android.

Upvotes: 1

Views: 194

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191681

on the Contract class, the members to create and maintain the database are private strings (SQL_CREATE_ENTRIES and SQL_DELETE_ENTRIES). Defining the DBHelper class on another file, I just cannot access that members the way it appears on the documentation (the FeedReaderDbHelper class just use them as if they were inside it scope)

Those private fields are supposed to be placed into the SQLiteOpenHelper class. They are used within the scope of that class to create and drop the table that is managed by that class. You should not be using those SQL strings elsewhere.

For example,

public class FeedReaderDbHelper extends SQLiteOpenHelper {

    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ",";
    private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + FeedEntry.TABLE_NAME + " (" +
        FeedEntry._ID + " INTEGER PRIMARY KEY," +
        FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
        FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP +
        ... // Any other options for the CREATE command
        " )";

    private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;

    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "FeedReader.db";

    public FeedReaderDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRIES);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
        db.execSQL(SQL_DELETE_ENTRIES);
        onCreate(db);
    }

    /* Inner class that defines the table contents */
    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";
        ...
    }

}

Upvotes: 0

vbry
vbry

Reputation: 78

Put SQL_CREATE_ENTRIES and SQL_DELETE_ENTRIES in the DBHelper class.

From the docs,

"A contract class is a container for constants that define names for URIs, tables, and columns."

only the schema of the database should be put in there. Logic pertaining to altering the database should be put inside the DBHelper class.

Upvotes: 3

Related Questions