What to display a ProgressDialog when my database is created. This is only showed once.
I have this code in my activity:
baseDados.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor query = baseDados.getData(1,0,null);
MyAdapt cursorAdapter = new MyAdapt(this, query,0);
listContent.setAdapter(cursorAdapter);
On the first run, database is created and populated and the result of the query is shown on a listview. The code above is used can be used again but this time, the database is not created because it is already created.
On the internet, I saw that the best way to achieve is to use AsyncTaks but I’m having trouble in showing the progressdialog and errors on creating the database if I use AsyncTaks.
I’ve created a class to handle my database and there created my AsyncTaks.
Here is what I’ve done:
public class Database {
private DbHelper DBHelper;
private final Context Context;
private SQLiteDatabase BaseDados;
static Context ctx;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, BD_NAME, null, BD_VERSION);
ctx = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
new loadDB().execute(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVesion) {
// Drop table if table exists
(....)
// creates new tables and data
onCreate(db);
}
public class loadDB extends AsyncTask<SQLiteDatabase, Void, Integer> {
ProgressDialog progresso;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progresso = ProgressDialog.show(ctx, "INFO", "Instaling for the first time") );
}
@Override
protected Integer doInBackground(SQLiteDatabase... params) {
SQLiteDatabase db = params[0];
// Code to create the tables and populate them
// Lots of code like below
db.execSQL("SQL statement goes here");
return 1;
}
@Override
protected void onPostExecute(Integer result) {
// Dismiss the ProgressDialog
progresso.dismiss();
super.onPostExecute(result);
}
}
}
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[]) {
// querys performed on the database
}
}
I have two problems.
Problem 1
ProgressDialog is never shown although it takes almost five seconds to create and populate my database
Problem 2
At some point, the app crashes and the database is not correctly created because the querys give error no such table
The code for creating an populating the database is OK because if in the method
@Override
public void onCreate(SQLiteDatabase db) {
new loadDB().execute(db);
}
I remove new loadDB().execute(db); and put this:
@Override
public void onCreate(SQLiteDatabase db) {
// SQL code that was on `doInBackground(SQLiteDatabase... params)`
}
Can you tell me what am I doing wrong?
You cannot use an AsyncTask to initialize a db inside constructor, especially because immediately after you call constructor you are calling
The AsyncTask would not have been run, so your db would not have been created, but you are trying to get a writable database. A better design is to make everything related to databases synchronous, and then use all db related methods in one Asynctask.
Also i think it is bad to mix UI and db operations like you have done. You will have to redesign the whole thing