Database
的类,其中包含一个private 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();
}
我有两个问题。
第一个问题:
由于这可以从4个不同的活动访问,那么我该如何获取上下文?
制作中:
Context context = getAplicationContext();
第二点:
为什么我不能使用来自strings.xml的字符串? 我无法完成这个操作:
ProgressDialog progresso = ProgressDialog.show(context, getString(R.string.driProgressTitle), getString(R.string.driProgressMessage) );
更新
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);
}
}
}
使用上面的代码时,我得到了这个错误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();
中。
doInBackground()
方法中触及任何与GUI相关的内容。重写onPreExecute()
并在其中调用ProgressDialog.show()
。 - S.D.new loadDB().execute();
? - FavolasDatabase
类的onCreate(SQLiteDatabase db)
中执行new loadDB().execute();
时出现了错误: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).
在我的主类/活动的onCreate(Bundle savedInstanceState)
中执行new loadDB().execute();
时出现了错误:loadDB cannot be resolved to a type
。感谢您的帮助。 - Favolas