在我的应用程序中,当我单击按钮时,它有时会显示进度对话框,有时在单击按钮时不显示进度对话框。
异步任务代码为:
public class LoadData extends AsyncTask<Void, Void, Void>
{
ProgressDialog pd;
@Override
protected void onPreExecute()
{
pd = ProgressDialog.show(MainActivity.this, "", "Loading...");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
LoadActivities(); // function to load data from url
}
});
return null;
}
@Override
protected void onPostExecute(Void unused)
{
pd.dismiss();
}
}
并在按钮单击事件上将其称为:
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new LoadMoreData().execute(null);
}
});