[英]Android: How do I create an object of a database admin class and then use it?
I am working on an Android project where I have a central database for the application and it is accessed/ input into from different activities within the same app. 我正在一个Android项目中工作,在该项目中我有一个适用于该应用程序的中央数据库,可以从同一应用程序中的不同活动对其进行访问/输入。 I am still a bit of a noob when it comes to Android and have followed various tutorials, but only to get myself slightly stuck :/ 当我谈到Android时,我还是有点菜鸟,并且已经看过各种教程,但是只是让自己稍微陷入困境:/
The problem I am having is this , if the code posted below is my "base" class that is the one I will be using via objects and methods to access, how do I create an object with the correct parameters so as to make it work? 我遇到的问题是 ,如果下面发布的代码是我的“基础”类,该类是我将通过对象和方法访问的类,那么如何创建具有正确参数的对象以使其正常工作?
I try to create an object of the DatabaseAdmin class in a different class with the following syntax: DatabaseAdmin db = new DatabaseAdmin(); 我尝试使用以下语法在其他类中创建DatabaseAdmin类的对象:DatabaseAdmin db = new DatabaseAdmin(); But it is saying I need to add the parameter, context. 但这是说我需要添加参数context。 When I input the parameter as: DatabaseAdmin db = new DatabaseAdmin(getApplicationContext()); 当我输入以下参数时:DatabaseAdmin db = new DatabaseAdmin(getApplicationContext()); The app keeps force closing with multiple errors, so I am thinking this is not the solution. 该应用程序会强制关闭并出现多个错误,因此我认为这不是解决方案。
Ideas? 想法? How do I create an object that will have access to this class with all of the respective methods available to it and afterwards, do I need to do anything abnormal to utilize/ call it? 如何创建一个可以使用该类的所有相应方法访问该类的对象,之后,我是否需要做任何异常操作来利用/调用它?
(PS, apologies if I included too much code, tried to trim it down, not sure if something was important that might be a clue. If it is too much code, please let me know and I will trim it further) (PS,如果我包含太多代码,则表示歉意,请尝试对其进行精简,不确定是否有重要的提示可能是线索。如果代码过多,请告诉我,我将作进一步的精简)
Here is my code: 这是我的代码:
//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);
}
}
Try using the singleton model: 尝试使用单例模型:
Your App class (register in manifest) : 您的App类(在清单中注册):
class MyApp extends Application {
private static MyApp mInstance;
void onCreate() {
mInstance = this;
}
public Context context() { return mInstance.getApplicationContext(); }
}
Your database class: 您的数据库类:
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() {...}
}
}
Then from anywhere to access your databases: 然后从任何地方访问您的数据库:
MyDatabase.INSTANCE.someMyQuery();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.