2011-09-27 59 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] 就是你要找的 –

相关问题