我正在一个Android项目中工作,在该项目中我有一个适用于该应用程序的中央数据库,可以从同一应用程序中的不同活动对其进行访问/输入。当我谈到Android时,我还是有点菜鸟,并且已经看过各种教程,但是只是让自己稍微陷入困境:/
我遇到的问题是,如果下面发布的代码是我的“基础”类,该类是我将通过对象和方法访问的类,那么如何创建具有正确参数的对象以使其起作用?
我尝试使用以下语法在其他类中创建DatabaseAdmin类的对象:
DatabaseAdmin db =新的DatabaseAdmin();
但这是说我需要添加参数context。
当我输入参数为:
DatabaseAdmin db =新的DatabaseAdmin(getApplicationContext());
该应用程序会强制关闭并出现多个错误,因此我认为这不是解决方案。
有想法吗?如何创建一个可以使用该类的所有相应方法访问该类的对象,之后,我是否需要做任何异常操作来利用/调用它?
(PS,如果我包含太多代码,则表示歉意,请尝试对其进行精简,不确定是否有重要的提示可能是线索。如果代码过多,请告诉我,我将作进一步的精简)
这是我的代码:
//imports Cropped for space saving
//This class manages the database for the entire application
public class DatabaseAdmin extends Activity
{
DatabaseHelper dbhelper;
Context ctx;
//Create an object of the SQLite database which we can use to open and close it
SQLiteDatabase db;
//SQL Variables
public static final String TABLE_NAME = "table";
public static final String COLUMN_NAME_AT_RISK = "at_risk";
public static final String TEXT_TYPE = " TEXT";
public static final String COMMA = ",";
//String to create the database
public static final String SQL_CREATE_DATABASE =
"CREATE TABLE " +
TABLE_NAME + " (" +
COLUMN_NAME_AT_RISK + " REAL 1417"
" )";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "RSRToolbox.db";
public DatabaseAdmin(Context ctx) {
this.ctx = ctx;
dbhelper = new DatabaseHelper(ctx);
}
//DatabaseHelper
private class DatabaseHelper extends SQLiteOpenHelper {
//Constructor for DatabaseHelper
public DatabaseHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
//Creates database
try {
db.execSQL(SQL_CREATE_DATABASE);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST commissions");
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
//Method to open the database
public DatabaseAdmin openDB(){
db = dbhelper.getWritableDatabase();
return this;
}
//Method for closing the database
public void closeDB(){
dbhelper.close();
}
//Method to insert data into the database
public long InsertData(String column_name, String value){
ContentValues content = new ContentValues();
content.put(column_name, value);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor getData(String[] column_name){
return db.query(TABLE_NAME, column_name, null, null, null, null, null);
}
}
最佳答案
尝试使用单例模型:
您的App类(在清单中注册):
class MyApp extends Application {
private static MyApp mInstance;
void onCreate() {
mInstance = this;
}
public Context context() { return mInstance.getApplicationContext(); }
}
您的数据库类:
public MyDatabase {
public final MyDatabase INSTANCE = new MyDatabase();
private DbHelper mHelper;
public Cursor someMyQuery() {
return mHelper.query(...);
}
private MyDatabase() {
mHelper = new DbHelper(MyApp.context());
...
}
private static class DbHelper extends SQLiteOpenHelper {
onCreate() {...}
onUpdate() {...}
}
}
然后从任何地方访问您的数据库:
MyDatabase.INSTANCE.someMyQuery();