简体   繁体   English

Android-进度栏未更新

[英]Android - Progress Bar not updating

I have a thread which updates a data into SQLite DB after fetching it from a txt file, I want to show a dialog box with its progress updating accordingly. 我有一个线程可以从txt文件中提取数据后将数据更新到SQLite DB中,我想显示一个对话框,其进度将相应地更新。

The following code works correctly, even the runOnUiThread also works for dismiss, but Progress is being not updates. 以下代码正确运行,即使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();

runOnUiThread wouldn't work here, you should use a Handler . runOnUiThread在这里不起作用,您应该使用Handler This goes in the method were you define your thread . 这是在您定义thread时使用的方法。

final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int percent = msg.what;
        dialog.setProgress((int) percent);
        if (percent == 100) dialog.dismiss();
    }
};

Now from the thread you can call the handler : 现在,您可以从thread中调用handler

// ...
final float percent = (wordsSelected/totalWords) * 100;
Log.d("PROGRESS", Float.toString((int) percent));
handler.sendEmptyMessage((int) percent));
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM