Showing progress dialog while loading database in Android
<p>I am loading a database without problems in a <code>SQLiteOpenHelper&gt;&gt;onCreate(SQLiteDatabase)</code> method. </p>

<p>However, when I try to wrap the loading of the database in an <code>AsyncTask</code> in order to show a progress bar, I keep receiving the message that the database is closed(!). What is the reason of that ?</p>

<p>As an example, this code works fine :</p>

<pre><code>@Override
public void onCreate(SQLiteDatabase db) { 
    ...
    String sqlStatement = ... //content of file with sql
    for (String sqlStatement : sqlCode.split(";")) {
        db.execSQL(sqlStatement);
    }
}
</code></pre>

<p>But if I wrap the same code in a class (indirectly) extending <code>AsyncTask</code>, I will receive the error message saying that the database is closed :</p>

<pre><code>@Override
public void onCreate(SQLiteDatabase db) { 
    new LoadDBTask(context, db, progressDialog, alertDialog).execute(new Integer[] {scriptId});
}
</code></pre>

<p><code>LoadDBTask</code> extends <code>ProgressAsyncTask</code>, that at it turns extends from <code>AsyncTask</code>. It wraps the same code than above. It also takes care of showing the progress bar. </p>

<p>Here the code for <code>LoadDBTask</code> :</p>

<pre><code>public class LoadDBTask extends ProgressAsyncTask&lt;Integer&gt; {

    protected Context context;
    protected SQLiteDatabase db;

    public LoadDBTask(Context context, SQLiteDatabase db, ProgressDialog progressDialog, AlertDialog alertDialog) {
        super(progressDialog, alertDialog);
        this.context = context;
        this.db = db;
    }

    public boolean loadScript(String sqlCode) {
        for (String sqlStatement : sqlCode.split(";")) {
            sqlStatement = sqlStatement.trim();
            if(!sqlStatement.isEmpty()) {
                try {
                    db.execSQL(sqlStatement);
                } catch (Exception e) {  
                    Log.e(getClass().getSimpleName(), "Problem executing SQL statement: "+sqlStatement,e);
                    return false;
                }
            }   
        }
        return true;
    }

    @Override
    protected Boolean doInBackground(Integer... params) {
            int scriptId = params[0];
            String sqlCode;
            try {
                sqlCode = ResourceUtil.getFileContent(context.getResources(), scriptId);
                return loadScript(sqlCode);
            } catch (IOException e) {
                Log.e(getClass().getSimpleName(), "Error reading script file: ",e);
                return false;  
            } 
    }
}
</code></pre>

<p>And for completeness, here the code of <code>ProgressAsyncTask</code> :</p>

<pre><code>public abstract class ProgressAsyncTask&lt;Params&gt; extends AsyncTask&lt;Params, Integer, Boolean&gt; {

protected ProgressDialog progressDialog;
protected AlertDialog alertDialog;

public ProgressAsyncTask(ProgressDialog progressDialog, AlertDialog alertDialog) {
    this.progressDialog = progressDialog;
    this.alertDialog = alertDialog;
}


@Override
protected void onProgressUpdate(Integer... changed) {
    if(progressDialog != null)
        progressDialog.setProgress(changed[0]);

}

@Override
protected void onPreExecute() {
    if(progressDialog != null)
        progressDialog.show();
}

@Override
protected void onPostExecute(Boolean result) {
    if(progressDialog.isShowing())
        progressDialog.dismiss();
    if(!result) {
        if(alertDialog != null)
            alertDialog.show();
    }
}
}
</code></pre>
