Android进度对话显示得这么晚了?

时间:2011-11-13 21:34:06

标签: android progressdialog

这是问题

@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方法。

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

由于

1 个答案:

答案 0 :(得分:1)

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();