2013-04-23 56 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