Android get context in database
android, android-context, database
Solution
Don't you have access to the Context from the constructor?
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)
EDIT:
Your code:
public DbHelper(Context context) {
super(context, BD_NAME, null, BD_VERSION);
// TODO Auto-generated method stub
}
You have access to the context at this point.. can you just do something like add a field called context then do this:
Context context;
public DbHelper(Context context) {
super(context, BD_NAME, null, BD_VERSION);
// TODO Auto-generated method stub
this.context = context;
}
Now, you have access to the context anywhere in the class? Assuming that the constructor is called before onCreate.
Problem
I have one database and I've created a `Database` class that has a `private static class DbHelper extends SQLiteOpenHelper` to help me manage my database. This database is access from four different activities. I have this `public void onCreate(SQLiteDatabase db)` that it's what creates my database tables and adds values to those tables. Since the program only enter here the first time the user runs the app, I want to create a `ProgressDialog`. This is what I have: ``` Override public void onCreate(SQLiteDatabase db) { ProgressDialog progresso = ProgressDialog.show(context, "Info", "Loading DB" ); // Code to create the tables and add the values progress.dismiss(); } ``` Got 2 questions. First: Since this can be accessed from 4 different activities how can I get the context? Making: ``` Context context = getAplicationContext(); ``` Second: Why can't I use strings from strings.xml? I can't make this: ``` ProgressDialog progresso = ProgressDialog.show(context, getString(R.string.driProgressTitle), getString(R.string.driProgressMessage) ); ``` UPDATE ``` public class Database { ProgressDialog progresso; private DbHelper DBHelper; private final Context Context; private SQLiteDatabase BaseDados; private static class DbHelper extends SQLiteOpenHelper { public DbHelper(Context context) { super(context, BD_NAME, null, BD_VERSION); // TODO Auto-generated method stub } @Override public void onCreate(SQLiteDatabase db) { // Whanted to show here the progress dialog new loadDB().execute(); // Creates the table and adds the values } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVesion) { db.execSQL("DROP TABLE IF EXISTS MYTABLE"); onCreate(db); } } public Database(Context c) { Context = c; } public Database open() throws SQLException { DBHelper = new DbHelper(Context); BaseDados = DBHelper.getWritableDatabase(); return this; } public void close() { DBHelper.close(); } public Cursor getData(int tipo, long groupId, String query[]) { // Performs the querys to my database } return null; } public class loadDB extends AsyncTask<Void, Void, Integer> { @Override protected Integer doInBackground(Void... params) { progresso = ProgressDialog.show(Context, Context.getString(R.string.ProgressTitle), Context.getString(R.string.ProgressMessage) ); return 1; } @Override protected void onPostExecute(Integer result) { progresso.dismiss(); super.onPostExecute(result); } } } ``` With the above code I'm getting this error `No enclosing instance of type Database is accessible. Must qualify the allocation with an enclosing instance of type Database (e.g. x.new A() where x is an instance of Database).` in `new loadDB().execute();`.