Android get context in database

13,786

Solution 1

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.

Solution 2

The onCreate(SQLiteDatabase) method only gets called the first time you make a call to getReadableDatabase() or getWritableDatabase (i.e. the first time it is opened), not when the app is first run (of course you could first this to happen by simply opening the database the closing it).

I'm not sure giving it the Application context will work since the progressDialog will need to be displayed in the currently running Activity.

Instead of trying to display progress bars for precisely when the database is first accessed, have your current Activity display one any time it access the database itself. That way the first access to the database is masked by regular access to it.

If you don't want to display progress bars for all DB access, then again you can just open and close the database and have your Activity display a progress bar before you open it, and remove the progress bar when you close it (not doing anything with the database except let onCreate() run).

Unless your database structures are really big, you probably won't even see the progress bar. It doesn't take very long to create the tables.

Solution 3

Use Loader for reading data from DB. Don't clutter background classes with UI stuff

onCreateLoader(.. 
   //you can start progress

onLoadFinished(..
   // you can stop it

Also you read data from background thread which is recommended.

Share:
13,786
Favolas
Author by

Favolas

Updated on June 27, 2022

Comments

  • Favolas
    Favolas about 2 years

    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();.