Android 进度对话框显示这么晚?

发布于 2024-12-15 04:29:17 字数 1200 浏览 0 评论 0原文

问题

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     progressDialog = ProgressDialog.show(ContactMainActivity.this, "", 
            "Loading. Please wait...",true);
     setContentView(R.layout.contactlist);
        contactListView=getListView();
        contactListView.setTextFilterEnabled(true);
        contactListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


Thread t =  new Thread() {

        public void run() {

        try{
            runOnUiThread(new Runnable() {
                 public void run() {



            //stuff that updates ui
                     queryAllRawContacts();
                     progressDialog.dismiss();
                }
            });


            registerForContextMenu(contactListView);
        } catch (Exception e) {

        Log.e("tag", e.getMessage());

        }

        // dismiss the progress dialog



        }

        };
        t.start();



}

这是我的活动类的 onCreate 方法的

。此活动实际上是选项卡小部件的一个选项卡。因此,当我单击此选项卡并运行该活动时,它会等待大约 2 秒,然后显示类似 10 毫秒的进度对话框,并使用数据更新 UI。并关闭进度对话框。我想要实现的是,一旦单击该选项卡,就会显示进度对话框,并且加载数据大约需要 2-3 秒。加载并更新 UI 后,进度条将消失。

谢谢

Here is the problem

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     progressDialog = ProgressDialog.show(ContactMainActivity.this, "", 
            "Loading. Please wait...",true);
     setContentView(R.layout.contactlist);
        contactListView=getListView();
        contactListView.setTextFilterEnabled(true);
        contactListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);


Thread t =  new Thread() {

        public void run() {

        try{
            runOnUiThread(new Runnable() {
                 public void run() {



            //stuff that updates ui
                     queryAllRawContacts();
                     progressDialog.dismiss();
                }
            });


            registerForContextMenu(contactListView);
        } catch (Exception e) {

        Log.e("tag", e.getMessage());

        }

        // dismiss the progress dialog



        }

        };
        t.start();



}

this is onCreate method of my activity class.

This activity actually one tab of a tabwidget. So when I click this tab and run the activity, it waits about 2 seconds and then show progress dialog like 10 millisec and updates the UI with the data. and dismiss progress dialog. What I would like to achieve is that as soon as the tab is clicked, the progress dialog will be shown and it takes about 2-3 seconds to load data. after loading and updating the UI the progress bar will be dismissed.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-12-22 04:29:17

queryAllRawContacts() 不需要在 UI 线程上运行。将您的代码更改为以下内容:

Thread t =  new Thread() {
  public void run() {
    queryAllRawContacts();
    try{
      runOnUiThread(new Runnable() {
        public void run() {
          //stuff that updates ui     
          progressDialog.dismiss();
        }
      });
      registerForContextMenu(contactListView);
    } catch (Exception e) {

    Log.e("tag", e.getMessage());

    }

    // dismiss the progress dialog

    }

  };
t.start();

queryAllRawContacts() needs to not be run on the UI thread. Change your code to the following:

Thread t =  new Thread() {
  public void run() {
    queryAllRawContacts();
    try{
      runOnUiThread(new Runnable() {
        public void run() {
          //stuff that updates ui     
          progressDialog.dismiss();
        }
      });
      registerForContextMenu(contactListView);
    } catch (Exception e) {

    Log.e("tag", e.getMessage());

    }

    // dismiss the progress dialog

    }

  };
t.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文