使用 AsyncTask 选择和取消选择复选框时的 Android ProgressDialog

发布于 2024-10-17 10:41:54 字数 2082 浏览 2 评论 0原文

我是 Android 新手,在使用 AsyncTask 类时遇到一些问题。我有很多(超过 100 个)复选框,我想使用“全选”复选框来选择或取消选择它们。 不幸的是,这需要相当长的时间,对于用户来说很明显,所以我想使用 ProgressDialog 来显示选择正在进行中。

现在我知道 UI 元素的更新应该在 UI 线程中完成,因此我在 onProgressUpdate 方法中有选择代码,但只有在处理(选择或取消选择所有复选框)完成后,ProgressDialog 才会显示。 ..

这应该怎么做?有没有任何解决方法或任何其他解决方案?我还尝试检查 doInBackground() 方法中的所有复选框,但出现很多奇怪的错误(我猜这是因为 UI)。

这是我的代码:

customPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

      @Override
      public boolean onPreferenceChange(final Preference preference, final Object object) {
          new CbSelection().execute(preference);
          return true;
      }
});


private class CbSelection extends AsyncTask<Preference, Boolean, Void> {   

          private ProgressDialog Dialog = new ProgressDialog(Preferences.this);

          @Override
          protected void onPreExecute() {   
              Dialog.setMessage("Please wait..."); 
              Dialog.setCanceledOnTouchOutside(true);
              Dialog.setCancelable(true);
              Dialog.show();   
          }   

          @Override
          protected Void doInBackground(Preference... pref) {   

              PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);

              Preference preference = pref[0];
              if(preference.getKey().compareTo("select_all") == 0){
                  publishProgress(true);
              } 
              return null; 
          }   

          @Override
          protected void onProgressUpdate(Boolean... ok) {
              if(ok[0]== true) {
                  PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);
                  for(int i = 0; i < screen.getPreferenceCount(); ++i) {
                      //Dialog.show();
                       /* select or deselect all checkboxes here... */
                  }

              }
          }

          @Override
          protected void onPostExecute(Void v) {   
              Dialog.cancel(); 
          }   
} 

I'm a newbie to Android and I'm having some problems with the AsyncTask class. I have a lot of (over 100) check boxes which I want to select or deselect with a "select all" check box.
Unfortunately this takes quite some time, noticeable for the user, so I want to use the ProgressDialog to show that the selecting is in progress.

Now I know that updates of the UI elements are supposed to be done in the UI thread so I have the selecting code in the onProgressUpdate method but the ProgressDialog doesn't show up not until the processing (select or deselecting all checkboxes) is finished...

How is this supposed to be done? Is there any workaround or any other solution? I also tried to go through all the check boxes in the doInBackground() method but I get lots of weird errors (I guess it's because of the UI).

This is my code:

customPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

      @Override
      public boolean onPreferenceChange(final Preference preference, final Object object) {
          new CbSelection().execute(preference);
          return true;
      }
});


private class CbSelection extends AsyncTask<Preference, Boolean, Void> {   

          private ProgressDialog Dialog = new ProgressDialog(Preferences.this);

          @Override
          protected void onPreExecute() {   
              Dialog.setMessage("Please wait..."); 
              Dialog.setCanceledOnTouchOutside(true);
              Dialog.setCancelable(true);
              Dialog.show();   
          }   

          @Override
          protected Void doInBackground(Preference... pref) {   

              PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);

              Preference preference = pref[0];
              if(preference.getKey().compareTo("select_all") == 0){
                  publishProgress(true);
              } 
              return null; 
          }   

          @Override
          protected void onProgressUpdate(Boolean... ok) {
              if(ok[0]== true) {
                  PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);
                  for(int i = 0; i < screen.getPreferenceCount(); ++i) {
                      //Dialog.show();
                       /* select or deselect all checkboxes here... */
                  }

              }
          }

          @Override
          protected void onPostExecute(Void v) {   
              Dialog.cancel(); 
          }   
} 

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

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

发布评论

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

评论(1

寻找我们的幸福 2024-10-24 10:41:54

如果在执行 AsyncTask 之前创建并显示对话框会怎么样?这就是它通常的使用方式:

private ProgressDialog Dialog;
customPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

      @Override
      public boolean onPreferenceChange(final Preference preference, final Object object) {
          Dialog = new ProgressDialog(Preferences.this);
          Dialog.setMessage("Please wait..."); 
          Dialog.setCanceledOnTouchOutside(true);
          Dialog.setCancelable(true);
          Dialog.show();   

          new CbSelection().execute(preference);
          return true;
      }
});
private class CbSelection extends AsyncTask<Preference, Boolean, Void> {   

          @Override
          protected Void doInBackground(Preference... pref) {   
              PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);

              Preference preference = pref[0];
              if(preference.getKey().compareTo("select_all") == 0){
                  publishProgress(true);
              } 
              return null; 
          }   

          @Override
          protected void onProgressUpdate(Boolean... ok) {
              if(ok[0]== true) {
                  PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);
                  for(int i = 0; i < screen.getPreferenceCount(); ++i) {
                      /* select or deselect all checkboxes here... */
                  }

              }
          }

          @Override
          protected void onPostExecute(Void v) {   
              Dialog.dismiss(); 
          }   
} 

What if you create and show the dialog before executing the AsyncTask? That's the way it usually is used:

private ProgressDialog Dialog;
customPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

      @Override
      public boolean onPreferenceChange(final Preference preference, final Object object) {
          Dialog = new ProgressDialog(Preferences.this);
          Dialog.setMessage("Please wait..."); 
          Dialog.setCanceledOnTouchOutside(true);
          Dialog.setCancelable(true);
          Dialog.show();   

          new CbSelection().execute(preference);
          return true;
      }
});
private class CbSelection extends AsyncTask<Preference, Boolean, Void> {   

          @Override
          protected Void doInBackground(Preference... pref) {   
              PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);

              Preference preference = pref[0];
              if(preference.getKey().compareTo("select_all") == 0){
                  publishProgress(true);
              } 
              return null; 
          }   

          @Override
          protected void onProgressUpdate(Boolean... ok) {
              if(ok[0]== true) {
                  PreferenceScreen screen = (PreferenceScreen)getPreferenceScreen().getRootAdapter().getItem(2);
                  for(int i = 0; i < screen.getPreferenceCount(); ++i) {
                      /* select or deselect all checkboxes here... */
                  }

              }
          }

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