进度对话框显示为时已晚 [英] Progress dialog shows too late

查看:243
本文介绍了进度对话框显示为时已晚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要做如下的应用程序:

I have an app that I want to do the following:


  1. 显示一个按钮和TextView的一个活动。

  2. 用户点击按钮,程序显示一个进度对话框。

  3. 应用程序调用Web服务来获取列表。

  4. 进度对话框是隐藏的,会出现一个列表选择对话框中显示的检索列表。

  5. 用户选择列表中的项目之一。

  6. 项目显示了在TextView中。

的问题是,这种情况发生:

The problem is that this happens:


  1. 显示一个按钮和TextView的一个活动。

  2. 用户点击按钮,按钮状态变为选中。

  3. 几秒钟后,出现的列表中选择对话框,显示检索列表。

  4. 用户选择列表中的项目之一。

  5. 进度对话框将显示几秒钟,然后是隐藏的。

  6. 项目显示在TextView中。

该网站的服务是与在上preExecute()方法显示的进度对话框的AsyncTask的执行,并在onPostExecute(辞退)方法:

The web service is executed in an AsyncTask with the progress dialog shown in the onPreExecute() method and dismissed in the onPostExecute() method:

public class WebService extends AsyncTask<Void, Void, Boolean> {

  public void onPreExecute() {
    _progressDialog = ProgressDialog.show(_context, "", message);
  }

  protected Boolean doInBackground(Void... params) {
    try {
      // Execute web service
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    return true;
  }

  protected void onPostExecute(Boolean result) {
    _progressDialog.hide();
  }
}

code执行Web服务并显示对话框:

Code to execute web service and show dialog:

WebService ws= new WebService();
ws.execute();

// Web service saves retrieved list in local db

// Retrieve list from local db

AlertDialog.Builder db = new AlertDialog.Builder(context);
db.setTitle("Select an Item");
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(context,
        android.R.layout.simple_selectable_list_item, list);
db.setAdapter(listAdapter, null);
db.show();

我需要的东西添加到code中的列表选择对话框之前,确保进度对话框中显示?

Do I need to add something to the code to make sure the progress dialog shows before the List Selection dialog?

感谢您提前。

推荐答案

我有同样的确切问题。我试图显示任务前的进度对话框,使用后可运行的,使用的是可运行runOnUiTask。没有什么是做。结果
该对话框总是显示后的工作已完成,或者没有显示在所有。

I had the same exact problem. I tried to show the Progress dialog before the task, using post runnable, using a runOnUiTask runnable. Nothing was quite doing it.
The dialog showed up always after the work was done, or did not show up at all.

解决方案我刚刚发现,它为我工作,事实证明,就是把在 doInBackground code到尝试{}赶上{} 块。

The solution I just found, and it works for me, proved, is to put the doInBackground code into a try{}catch{} block.

不要问我为什么它的工作原理。我的结论是,什么是错的Andr​​oid操作系统的设计,阻止在某些情况下调度消息messahe处理程序。并以某种方式的try / catch给出了处理刹车和消息发送。

Don't ask me why it works. My conclusion is that something is wrong in the Android Os design that blocks the messahe handler from dispatching messages under certain circumstances. And somehow try/catch gives the handlers a brake and the messages are dispatched.

请注意:我用try / catch块,即使我的code不抛出任何异常,只是用

NOTE: I used the try/catch block even if my code does not throw any Exceptions, just used

try {
     // code
}
catch( Exception e) { }

这篇关于进度对话框显示为时已晚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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