2011-09-27 40 views
0

我正在嘗試使用ProgressBar,它顯示加載數據庫的進度,該數據庫位於本地資產文件夾中。我不知道這是甚至可能的,因爲我使用之前從互聯網上下載文件的代碼。加載數據庫 - 如何在progressBar中顯示進度?

我認爲我必須得到數據庫的大小,但我不知道如何做到這一點。 conection.getContentLength()似乎不起作用。可能連接不包含地址assets/kneipen2.sqlite?

這是在OnCreate():

new LoadFilesTask().execute("assets/kneipen2.sqlite"); 

這是的AsyncTask內部類:

private class LoadFilesTask extends AsyncTask<String, Integer, Integer> { 

    @Override 
    protected Integer doInBackground(String... urls) {  

     //oeffneDatenbank(); 
     int count; 
     try { 

      myDbHelper = new DataBaseHelper(Start.this);  
      try {   
       myDbHelper.createDataBase();    
      } 
      catch (IOException ioe) {   
       throw new Error("Unable to create database"); 
      } 
      try { 

       myDbHelper.openDataBase(); 

      } catch (SQLException sqle) { 

       throw sqle; 
      }  

      URL url = new URL(urls[0]); 

      //Ignore this! Only used to show that operations can take a long time to complete... 
      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      int lenghtOfFile = conection.getContentLength(); 
      // downlod File 
      InputStream input = new BufferedInputStream(url.openStream()); 
      byte data[] = new byte[1024]; 

      long total = 0; 
      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int)(total*100/lenghtOfFile));  
      } 
      input.close(); 
     } catch (Exception e) {} 
     return null;    
    } 

因此,有未示出的進步,但在最後的數據庫正確加載。請幫幫我。

回答

2

您可以通過兩種方式來使用它: 要麼 preExecute()和postExecute(),其中在preExecute(的AsyncTask的方法),只是顯示THR progressDialog和postExecute( )解僱一樣。 或 使用處理程序,以顯示進度對話框,並解僱一樣...

希望u得到什麼,我說...

+0

是的,我得到你說的,謝謝。它工作得很好。所以,這個解決方案是一個很好的解決方案,它可以工作,但最後,如果可能的話,我會出於佈局的原因,仍然想讓progressBar顯示加載本地文件的進度。那可能嗎?如果沒有,有沒有辦法對progressDialog的外觀產生影響? – 10ff

2
ProgressDialog MyDialog = ProgressDialog.show(YourCurrentClass.this, "Please wait!" , "Loading..", true); 

這可能是幫助

+0

是的,這會有所幫助,謝謝。但是,您的意思是沒有辦法讓ProgressBar顯示加載區域設置文件的進度?由於ProgressDialog是一個很好的解決方案,但progressBar對我來說會更好,原因是設計/佈局。 – 10ff

+0

我覺得[ProgressBar] 就是你要找的 –

相關問題