【问题标题】:Android Progress Dialog shown up so late?Android 进度对话框出现这么晚?
【发布时间】:2011-11-13 21:34:05
【问题描述】:

问题来了

@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 后,进度条将被关闭。

谢谢

【问题讨论】:

    标签: android progressdialog


    【解决方案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();
    

    【讨论】:

    • 我按你说的改了。现在,当我运行它时,它会立即显示。然而,当它被解雇时,它会杀死应用程序。它不更新用户界面?
    • 您的应用程序一定有其他问题。你 logcat 怎么说?
    • 谢谢我已经成功了,基本上我移动了contactListView.setAdapter(adapter);进入基本上更新 Ui 的 runOnUithread 方法。现在就像一个魅力:) 谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多