title: Android ProgressDialog when creating Database using AsyncTask
body: <p>What to display a <code>ProgressDialog</code> when my <code>database</code> is created. This is only showed once.</p>
  
  <p>I have this code in my activity:</p>
  
  <pre><code>    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);
  </code></pre>
  
  <p>On the first run, database is created and populated and the result of the query is shown on a <code>listview</code>. The code above is used can be used again but this time, the database is not created because it is already created.</p>
  
  <p>On the internet, I saw that the best way to achieve is to use <code>AsyncTaks</code> but I'm having trouble in showing the progressdialog and errors on creating the database if I use <code>AsyncTaks</code>.</p>
  
  <p>I've created a class to handle my database and there created my <code>AsyncTaks</code>.</p>
  
  <p>Here is what I've done:</p>
  
  <pre><code>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&lt;SQLiteDatabase, Void, Integer&gt; {
              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
  }
      }
  </code></pre>
  
  <p>I have two problems.</p>
  
  <p><strong>Problem 1</strong></p>
  
  <p>ProgressDialog is never shown although it takes almost five seconds to create and populate my database</p>
  
  <p><strong>Problem 2</strong></p>
  
  <p>At some point, the app crashes and the database is not correctly created because the querys give error <code>no such table</code>
  The code for creating an populating the database is OK because if in the method  </p>
  
  <pre><code>@Override
  public void onCreate(SQLiteDatabase db) {
      new loadDB().execute(db);
  }
  </code></pre>
  
  <p>I remove <code>new loadDB().execute(db);</code> and put this:</p>
  
  <pre><code>@Override
  public void onCreate(SQLiteDatabase db) {
      // SQL code that was on `doInBackground(SQLiteDatabase... params)`
  }
  </code></pre>
  
  <p>Can you tell me what am I doing wrong?</p>