Android的异步任务ProgressDialog没有显示,直到后台线程完成 [英] Android ASync task ProgressDialog isn't showing until background thread finishes

查看:125
本文介绍了Android的异步任务ProgressDialog没有显示,直到后台线程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了它抓住了从URL的RSS提要,并使用SAX解析器坚持从XML每个项目到一个数组一个Android的活动。这一切工作正常,但是,正如所料,需要一点时间,所以我想用AsyncActivity做背景。我的code是如下:

I've got an Android activity which grabs an RSS feed from a URL, and uses the SAX parser to stick each item from the XML into an array. This all works fine but, as expected, takes a bit of time, so I want to use AsyncActivity to do it in the background. My code is as follows:

class AddTask extends AsyncTask<Void, Item, Void> {

    protected void onPreExecute() {
        pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
    }

    protected Void doInBackground(Void... unused) {
        items = parser.getItems();

        for (Item it : items) {
            publishProgress(it);
        }
        return(null);
    }

    protected void onProgressUpdate(Item... item) {
        adapter.add(item[0]);
    }

    protected void onPostExecute(Void unused) {
        pDialog.dismiss();
    }
  }

我在的onCreate调用()

new AddTask().execute();

项目= parser.getItems()工作正常 - 项目是包含从每个项目的数组列表XML。我现在面临的问题是,在启动活动中,ProgressDialog我在在preExecute创建() EM>的 doInBackground()方法完成。也就是说,我收到了黑屏,过了好一会,然后一个完全填充的列表中的项目。为什么会出现这种情况?为什么不UI绘制,所述ProgressDialog放映,分析器获取的项目和增量将其添加到列表中,则ProgressDialog解聘?

The line items = parser.getItems() works fine - items being the arraylist containing each item from the XML. The problem I'm facing is that on starting the activity, the ProgressDialog which i create in onPreExecute() isn't displayed until after the doInBackground() method has finished. i.e. I get a black screen, a long pause, then a completely populated list with the items in. Why is this happening? Why isn't the UI drawing, the ProgressDialog showing, the parser getting the items and incrementally adding them to the list, then the ProgressDialog dismissing?

推荐答案

这对我的作品

@Override
protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }

这篇关于Android的异步任务ProgressDialog没有显示,直到后台线程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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