Add the static keyword to your shared methods
public static void createTable()
Then use:
Data.createTable();
somewhere in another Class / Fragment / Activity.
I do so and it works.
Well, my "Data" is a normal Class, not an Activity, but I also have an Activity with shared methods in the very same way.
This is my dbTableCreate method:
// Create the database table if it doesn't exist
protected final static void dbTableCreate(final Context ctx)
{
SQLiteDatabase db = null;
try
{
db = ctx.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);
db.execSQL
(
"CREATE TABLE IF NOT EXISTS " + DB_TABLE +
" (date DATETIME PRIMARY KEY NOT NULL DEFAULT (CURRENT_DATE), " +
"score INTEGER DEFAULT (null));"
);
}
catch (final SQLiteException se)
{
System.out.println
(
ctx.getClass().getSimpleName() + "
" +
"Could not create the database"
);
se.printStackTrace();
}
finally
{
db.close();
}
}
And I call it like:
Context ctx = getApplicationContext();
CLS_DB.dbTableCreate(ctx);
The main difference between my class and yours is that mine is declared as
public final class CLS_DB
{
/* ----------------------------- Constants ------------------------------ */
private final static String DB_NAME = "pat.db";
private final static String DB_TABLE = "tests";
/* ------------------------------ Methods ------------------------------- */
And not as an Activity.
I'm not initializing the Class, and it has no constructor.
It's just a bin of shared (and not, for doing operations not seen anywhere else in my code) methods.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…