2013-04-23 50 views
0

我有一个奇怪的问题,正在创建一个对话框,并呼吁“秀()”,但不是在我的活动可见......ProgressDialog没有显示出来,尽管显示()被调用

的东西是它通过调用“作秀”,我已经看到了它在调试器,但没有...

这里是代码:

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();** 
} 

我在做什么坏事?

+0

我不知道太多关于ProgressDialog但也许尝试'myPd_bar.create ().show();'。不知道在这里是否需要'.create()'... – TronicZomB 2013-04-23 20:14:14

+1

甚至不允许! :)无论如何感谢 – Nhano 2013-04-24 17:25:08

+0

好吧,我也发现这个早上''.show()'将创建对话框并显示在一个!所以在这个例子中'.create()'是没有必要的。我曾经使用过的对话框的方式我从来没有遇到过,仅仅使用.show()没有.create会更好,所以我从来不知道这一点。 – TronicZomB 2013-04-24 17:29:40

回答

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); 
} 
1

您的对话框出现之前可能会被解雇。基本上你的循环没有足够长的时间来显示视图。

你有没有试过评论退出行,看看它是否出现呢?

+0

它在调试时并没有出现,我循环了10次,并且在句柄消息中有一个断点,但没有任何结果。 – Nhano 2013-04-23 21:21:02

+0

就像我说的你试图注释'myPd_bar.dismiss();'?你也应该使用'DialogFragment'来做到这一点。 – tyczj 2013-04-24 12:03:51

+0

抱歉tycj,因为活动被阻止了一段时间,我没有尝试它。现在我尝试了一下,发生了一件非常奇怪的事情。它显示出来,但是当它完成时,不是从一开始,也不是当我问到它时......它在完成时显示出来! 为什么我应该使用dialogfragment?这一个是我需要的确切功能,如果我使用片段,该应用程序不适用于3.0以下版本... 再次感谢 – Nhano 2013-04-24 17:20:30