2013-08-26 81 views
0

我想在我的活動中顯示進度條作爲對按鈕單擊的響應。 我讀了另一個問題,我應該使用異步任務,以顯示/不顯示進度條,但當我點擊按鈕進度條沒有正確顯示(它出現的時間應該少得多) 任何建議?無法正常顯示進度條

活動代碼:

public void chooseContactFromList(View view){ 
ProgressBar pBar = (ProgressBar) findViewById(R.id.progressBar1); 
circleActivity progressTask = (circleActivity) new circleActivity(pBar).execute(); 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
CharSequence[] cs=nameList.toArray(new CharSequence[nameList.size()]); 
builder.setTitle("Make your selection"); 
    builder.setItems(cs, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int item) { 
      reciverNumber = phoneList.get(item); 
     } 
    }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
    progressTask.cancel(true); 
} 

中的AsyncTask代碼:

public class circleActivity extends AsyncTask<Void, Void, Void> { 
private ProgressBar progressBar; 

public circleActivity(ProgressBar pBar) { 
    progressBar=pBar; 
} 

protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
    progressBar.setVisibility(View.VISIBLE); 
} 

@Override 
protected void onPostExecute(Void result) { 
    progressBar.setVisibility(View.INVISIBLE); 
} 


@Override 
protected void onProgressUpdate(Void ... progress) { 

} 

@Override 
protected Void doInBackground(Void... arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

}

感謝

回答

0

正如你什麼都不做的doInBackground(),進度條顯示爲只有幾分鐘。如果你真的想看到它,然後嘗試在doInBackground()做一些操作,這將需要一些時間。

例如。嘗試使用doInBackground中的Thread.sleep(1000);進行測試。

我建議你參考以下鏈接。

  1. http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html
  2. http://developer.android.com/reference/android/os/AsyncTask.html
+0

是啊,傻我的 感謝! –