我想在我的活动中显示一个进度条作为对按钮单击的响应。我在另一个问题中读到,我应该使用异步任务来显示/不显示进度条,但是当我单击按钮时,进度条没有正确显示(它出现的时间比它应该少得多)有什么建议吗?
活动代码:
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;
}
}
谢谢