尽管show()被调用,ProgressDialog仍未显示

时间:2013-04-23 20:10:31

标签: android progress-bar show

我遇到了一个奇怪的问题,一个Dialog正在创建并调用“show()”但在我的活动中不可见......

事情是它通过调用“show”,我在调试器中看到它,但没有...

这是代码:

    protected void initializeSpinners() {
    spnPlayLists = (Spinner) findViewById(R.id.spnLists);
    spnProviders = (Spinner) findViewById(R.id.spnProvider);
    spnProviders.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> _spinner, View _parent,
                int _pos, long _id) {
            if (_spinner == spnProviders) {
                String[] playListsId = core.getAllPlayListsFrom(_pos);
                int items = playListsId.length;
                **showProgressDialog(items);**
                String[] playListsNames = new String[items];
                String[] playListsThumbs = new String[items];
                playLists = new PlayList[items];
                for (int i = 0; i < items; i++) {
                    String id = playListsId[i];
                    PlayList playList = core.getPlayList(id, true);
                    playLists[i] = playList;
                    playListsNames[i] = playList.title;
                    playListsThumbs[i] = playList.thumb;
                    handle.sendEmptyMessage(i);
                }
                loadPlayLists(playListsNames, playListsThumbs);
                myPd_bar.dismiss();
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> _arg0) {
        }

    });

    ProvidersArrayAdapter providersAdapter = new ProvidersArrayAdapter(this);
    spnProviders.setAdapter(providersAdapter);
}

和调用的函数:

    private void showProgressDialog(int _items) {
    handle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            myPd_bar.setProgress(msg.what + 1);
        }
    };

    myPd_bar = new ProgressDialog(Intro.this);
    myPd_bar.setMessage("Loading....");
    myPd_bar.setTitle("Please Wait..");
    myPd_bar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    myPd_bar.setProgress(0);
    myPd_bar.setMax(_items);
    **myPd_bar.show();**
}

我做得不好......?

2 个答案:

答案 0 :(得分:1)

您的对话框可能会在出现之前被解除。基本上你的循环不够长,不能出现视图。

您是否尝试过去掉该关闭行,看看它是否会出现?

答案 1 :(得分:0)

最后我意识到了。我在微调器中持有活动线程,因此在完成之前它不会显示任何内容!

这是我的解决方案,使用asyntask(正确地说,我在播放列表加载和阻塞时严重使用它们)

protected void setupDialog() {
    handle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what < 0){
                loadPlayLists(playListsNames, playListsThumbs);
                myPd_bar.dismiss();
            } else {
                myPd_bar.setProgress(msg.what + 1);
            }
        }
    };

    myPd_bar = new ProgressDialog(Intro.this);
    myPd_bar.setMessage(WAIT_MESSAGE);
    myPd_bar.setTitle(WAIT_TITLE);
    myPd_bar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    myPd_bar.setProgress(0);
}

protected void initializeSpinners() {
    spnPlayLists = (Spinner) findViewById(R.id.spnLists);
    spnProviders = (Spinner) findViewById(R.id.spnProvider);
    spnProviders.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> _spinner, View _parent,
                int _pos, long _id) {
            if (_spinner == spnProviders) {
                String[] playListsId = core.getAllPlayListsFrom(_pos);
                int items = playListsId.length;
                myPd_bar.setMax(items);
                myPd_bar.show();
                new AsyncTask<String[], Integer, Long>(){

                    @Override
                    protected Long doInBackground(String[]... _playListsId) {
                        int items = _playListsId[0].length;
                        playListsNames = new String[items];
                        playListsThumbs = new String[items];
                        playLists = new PlayList[items];
                        for (int i = 0; i < items; i++) {
                            String id = _playListsId[0][i];
                            PlayList playList = core.getPlayList(id, true);
                            playLists[i] = playList;
                            playListsNames[i] = playList.title;
                            playListsThumbs[i] = playList.thumb;
                            publishProgress(i);
                        }
                        return null;
                    }

                    @Override
                    protected void onProgressUpdate(Integer... _progress) {
                        handle.sendEmptyMessage(_progress[0]);
                     }

                    @Override
                    protected void onPostExecute(Long _result) {
                        handle.sendEmptyMessage(-1);
                     }
                }.execute(playListsId);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> _arg0) {
        }

    });

    ProvidersArrayAdapter providersAdapter = new ProvidersArrayAdapter(this);
    spnProviders.setAdapter(providersAdapter);
}