我有一个线程在从 txt 文件中获取数据后将数据更新到 SQLite DB 中,我想显示一个对话框,其进度相应更新。
以下代码正常工作,即使 runOnUiThread 也适用于解雇,但 Progress 没有更新。
Thread thread = new Thread(){
@Override
public void run() {
Log.d("THREAD", "in run");
// TODO Auto-generated method stub
try {
Log.d("DATABASE", "Creating Database");
SQLiteDatabase db = openOrCreateDatabase("WordDatabase", MODE_PRIVATE, null);
Log.d("DATABASE", "Checking table exist");
Cursor tableName = db.rawQuery("SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name='Words'",null);
if(tableName.getCount() < 1){
Log.d("DATABASE", "Creating Table");
db.execSQL("CREATE TABLE IF NOT EXISTS Words (Word TEXT);");
ContentValues cv = new ContentValues();
float totalWords = 5000;// 144895;
float wordsSelected = 0;
Log.d("CONTENT", "Entering Values");
Scanner scanner = new Scanner(getApplicationContext().getAssets().open("english.txt"));
while(scanner.hasNext()){
int i=0;
while(scanner.hasNext() && i<500 ){
cv.put("Word", scanner.next());
db.insert("Words", null, cv);
i++;
wordsSelected++;
final float percent = (wordsSelected/totalWords)*100;
Log.d("PROGRESS", Float.toString((int)percent));
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog.setProgress((int) percent);
if(percent == 100) dialog.dismiss();
}
});
}
cv.clear();
Log.d("CONTENT", "inserting");
}
Log.d("CONTENT", "Completed");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
};
thread.start();