Android的runOnUiThread解释 [英] Android runOnUiThread explanation

查看:133
本文介绍了Android的runOnUiThread解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展示我的活动之一的onCreate()方法时进度对话框,所做的工作来填充在一个线程中完成的画面,然后关闭该进程对话框。

下面是我的onCreateMethod()

 对话框=新ProgressDialog(HeadlineBoard.this);
        dialog.setMessage(填充头条......);
        dialog.show();
        populateTable();
 

该populateTable方法包含我的线程和code以关闭该对话框,但由于某种原因。活动出现空白约10秒(做populateTable()的工作),然后我看到屏幕。我从来没有看到显示的对话框中,任何想法?

下面是populateTable()code:

  //添加一行到表格中传递的每个标题
私人无效populateTable(){
    新的Thread(){
        @覆盖
        公共无效的run(){
            //如果有故事,将它们添加到表
            对于(Parcelable currentHeadline:allHeadlines){
                addHeadlineToTable(currentHeadline);
            }
               尝试 {
                     // code在一个线程中运行
                     runOnUiThread(新的Runnable(){
                            @覆盖
                            公共无效的run(){
                                dialog.dismiss();
                            }
                     });
               }赶上(最终例外前){
                   Log.i(---,线程中的异常);
               }
        }
 }。开始();

}
 

解决方案

如果您已经有数据吗?对于(Parcelable currentHeadline:allHeadlines),那么你为什么这样做,在一个单独的线程

您需要查询在一个单独的线程中的数据,当它完成收集,然后调用UI线程上的populateTables方式:

 私人无效populateTable(){
    runOnUiThread(新的Runnable(){
        公共无效的run(){
            //如果有故事,将它们添加到表
            对于(Parcelable currentHeadline:allHeadlines){
                addHeadlineToTable(currentHeadline);
            }
            尝试 {
                dialog.dismiss();
            }赶上(最终例外前){
                Log.i(---,线程中的异常);
            }
        }
    });
}
 

I am trying to display a progress dialog during the onCreate() method of one of my activities, have the work done to populate the screen done in a thread, and then dismiss the progress dialog.

Here is my onCreateMethod()

dialog = new ProgressDialog(HeadlineBoard.this);
        dialog.setMessage("Populating Headlines.....");
        dialog.show();
        populateTable();

The populateTable method contains my thread and the code to dismiss the dialog, but for some reason. The activity comes up blank for about 10 secs(doing the populateTable() work), and then I see the screen. I never see the dialog displayed, any ideas?

Here is the populateTable() code:

//Adds a row to the table for each headline passed in
private void populateTable() {
    new Thread() {
        @Override
        public void run() {
            //If there are stories, add them to the table
            for (Parcelable currentHeadline : allHeadlines) {
                addHeadlineToTable(currentHeadline);
            }
               try {
                     // code runs in a thread
                     runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                            }
                     });
               } catch (final Exception ex) {
                   Log.i("---","Exception in thread");
               }
        }
 }.start();

}

解决方案

If you already have the data "for (Parcelable currentHeadline : allHeadlines)," then why are you doing that in a separate thread?

You should poll the data in a separate thread, and when it's finished gathering it, then call your populateTables method on the UI thread:

private void populateTable() {
    runOnUiThread(new Runnable(){
        public void run() {
            //If there are stories, add them to the table
            for (Parcelable currentHeadline : allHeadlines) {
                addHeadlineToTable(currentHeadline);
            }
            try {
                dialog.dismiss();
            } catch (final Exception ex) {
                Log.i("---","Exception in thread");
            }
        }
    });
}

这篇关于Android的runOnUiThread解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆