Android获取数据库中的上下文

Fav*_*las 3 database android android-context

我有一个数据库,我创建了一个Databaseprivate static class DbHelper extends SQLiteOpenHelper来帮助我管理我的数据库.

该数据库可以从四种不同的活动中访问.

我有这个public void onCreate(SQLiteDatabase db),它是创建我的数据库表并为这些表添加值的原因.由于程序仅在用户第一次运行应用程序时进入此处,因此我想创建一个ProgressDialog.

这就是我所拥有的:

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();
}
Run Code Online (Sandbox Code Playgroud)

有两个问题.

第一:

由于这可以从4个不同的活动中访问,我如何获得上下文?制造:

Context context = getAplicationContext();
Run Code Online (Sandbox Code Playgroud)

第二:

为什么我不能使用strings.xml中的字符串?我做不到这个:

ProgressDialog progresso = ProgressDialog.show(context, getString(R.string.driProgressTitle), getString(R.string.driProgressMessage) );
Run Code Online (Sandbox Code Playgroud)

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

与上面的代码我得到这个错误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).new loadDB().execute();.

Kir*_*Koa 8

你不能从构造函数访问Context吗?

SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)

SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) 
Run Code Online (Sandbox Code Playgroud)

编辑:

你的代码:

public DbHelper(Context context) {
        super(context, BD_NAME, null, BD_VERSION);
        // TODO Auto-generated method stub
    }
Run Code Online (Sandbox Code Playgroud)

此时您可以访问上下文..您可以执行类似添加称为上下文的字段之类的操作,然后执行以下操作:

Context context;
public DbHelper(Context context) {
        super(context, BD_NAME, null, BD_VERSION);
        // TODO Auto-generated method stub
        this.context = context;
    }
Run Code Online (Sandbox Code Playgroud)

现在,您可以访问班级中的任何位置的上下文?假设在onCreate之前调用构造函数.