2012-08-28 30 views
0

當我的database創建時,如何顯示ProgressDialog。這隻顯示一次。使用AsyncTask創建數據庫時的Android ProgressDialog

我有這樣的代碼在我的活動:

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); 

在第一次運行時,創建數據庫,填充和查詢的結果顯示在一個listview。使用上面的代碼可以再次使用,但是這次數據庫不會被創建,因爲它已經被創建。

在互聯網上,我看到最好的方法是使用AsyncTaks,但如果我使用AsyncTaks,則在創建數據庫時顯示progressdialog和錯誤信息時遇到問題。

我創建了一個類來處理我的數據庫,並創建了我的AsyncTaks

這裏是我做了什麼:

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 
} 
    } 

我有兩個問題。

問題1

ProgressDialog從未盡管它顯示需要差不多五秒鐘以創建和填充我的數據庫

問題2

在某一點上,應用程序崩潰和數據庫沒有正確創建,因爲查詢給出錯誤no such table 創建填充數據庫的代碼是OK,因爲如果在方法

@Override 
public void onCreate(SQLiteDatabase db) { 
    new loadDB().execute(db); 
} 

我刪除new loadDB().execute(db);,並把這個:

@Override 
public void onCreate(SQLiteDatabase db) { 
    // SQL code that was on `doInBackground(SQLiteDatabase... params)` 
} 

你能告訴我什麼我做錯了嗎?

回答

1

不能使用的AsyncTask來初始化內部構造一個數據庫,特別是因爲後立即調用構造您所呼叫

BaseDados = DBHelper.getWritableDatabase(); 

的的AsyncTask就不會被運行,所以你的數據庫不會被創建,但你正試圖獲得一個可寫的數據庫。更好的設計是使與數據庫相關的所有內容都同步,然後在一個Asynctask中使用所有與數據庫相關的方法。

此外,我認爲這樣做混合UI和數據庫操作是不好的,就像你做的一樣。你將不得不重新設計整個事情

+0

基本上Database.open是一旦DB存在必須只返回一個同步操作。 – njzk2

+0

感謝您的解釋 – Favolas

1

機會是你不需要顯示progressdialog都:

  1. 如果你的數據庫創建足夠短(大多數情況下),你可以做到這一點同步。
  2. 如果沒有,您可以嵌入預先存在的數據庫並將其複製。它更快,並且在文件副本上放置progressdialog非常簡單。見這個例子如何在您的APK中嵌入數據庫:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

+0

感謝您的解釋 – Favolas