2014-12-04 80 views
2

我知道这已被问到,但我有不同的设置。创建数据库时的进度条

我的SQLite数据库:

private void SetUpLibrary() { 
    ArrayList<String> results = new ArrayList<String>(); 
     //Declare SQLiteDatabase object 
     SQLiteDatabase sampleDB = null; 

     try { 
      //Instantiate sampleDB object 
      sampleDB = this.openOrCreateDatabase(db.dbname, MODE_PRIVATE, null); 
      //Create table using execSQL 

      try{ 
       //add table 
       sampleDB.execSQL("CREATE TABLE " + db.tblnames[0]+ " (disease_no INTEGER, disease_name VARCHAR, disease_desc VARCHAR, disease_symptoms VARCHAR, disease_control VARCHAR);"); 


       Toast.makeText(getApplicationContext(), "Application library has been set successfully!", Toast.LENGTH_LONG).show(); 
      }catch (SQLiteException se) { 
       } 

     } catch (SQLiteException se) { 
      Toast.makeText(getApplicationContext(), "Couldn't create or open the database", Toast.LENGTH_LONG).show(); 
     } 

} 

我的进度条:

progressBar = (ProgressBar) findViewById(R.id.progressBar1); 

     // Start long running operation in a background thread 
     new Thread(new Runnable() { 
     public void run() { 
      while (progressStatus < 30) { 
       progressStatus += 1; 
     // Update the progress bar and display the 
          //current value in the text view 
     handler.post(new Runnable() { 
     public void run() { 
      progressBar.setProgress(progressStatus); 
      // textView.setText(progressStatus+"/"+progressBar.getMax()); 
     } 
      }); 
      try { 
       // Sleep for 200 milliseconds. 
          //Just to display the progress slowly 
       Thread.sleep(200); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
     } 
     } 
     }).start(); 

而这里的XML:

<ProgressBar 
    android:id="@+id/progressBar1" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/bStart" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="42dp" 
    android:indeterminate="false" 
    android:max="30" 
    android:minHeight="50dp" 
    android:minWidth="200dp" 
    android:progress="1" /> 

我想问问如何使进度条的工作,同时设置建立SQLite数据库。我尝试了一些建议,但没有一个能够奏效。

+0

请不要再使用max 30 0到100,所以你可以处理完成百分比。那就是除非你在九月,四月,六月和十一月有一些实际值为30的东西。 – danny117 2014-12-04 20:06:32

+0

究竟你有什么问题?进度条上看不到进展情况?提供一些你在'handler.post(new Runnable(){...}'中使用的处理程序的详细信息。 – 2014-12-04 20:06:38

+0

@ danny117:是的,对不起,它应该是100. – 2014-12-07 18:54:35

回答

0

您必须从设置更新进度。见下面

private void SetUpLibrary() { 
    progressBar.setProgress(0); 

    ArrayList<String> results = new ArrayList<String>(); 
     //Declare SQLiteDatabase object 
     SQLiteDatabase sampleDB = null; 

     try { 
      //Instantiate sampleDB object 
      sampleDB = this.openOrCreateDatabase(db.dbname, MODE_PRIVATE, null); 
      progressBar.setProgress(50); 
      //Create table using execSQL 

      try{ 
       //add table 
       sampleDB.execSQL("CREATE TABLE " + db.tblnames[0]+ " (disease_no INTEGER, disease_name VARCHAR, disease_desc VARCHAR, disease_symptoms VARCHAR, disease_control VARCHAR);"); 

       progressBar.setProgress(100); 
       Toast.makeText(getApplicationContext(), "Application library has been set successfully!", Toast.LENGTH_LONG).show(); 
      }catch (SQLiteException se) { 
       } 

     } catch (SQLiteException se) { 
      Toast.makeText(getApplicationContext(), "Couldn't create or open the database", Toast.LENGTH_LONG).show(); 
     } 
    progressBar.setVisibility(View.Gone); 
}