0

我有一个奇怪的问题,正在创建一个对话框并调用“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();**
}

我在做什么坏事……?

4

2 回答 2

1

您的对话框可能在它出现之前就被关闭了。基本上,您的循环时间不够长,无法显示视图。

您是否尝试过注释掉解雇行并查看它是否出现?

于 2013-04-23T20:20:20.787 回答
0

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

这是我的解决方案,使用异步任务(正确地,我在播放列表加载和阻塞时使用它们很糟糕)

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);
}
于 2013-04-25T17:58:55.467 回答