下面是连接到对话框的代码部分。当他们按下按钮后,它应该出现,在出现之后,它应该处理数据,当它完成时,它应该隐藏起来。但它甚至没有出现?
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Prosimo počakajte da naloži podatke.");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
private Button.OnClickListener listener = new Button.OnClickListener() {
public void onClick(View v){
if(selectedClass >= 0){
dialog.show();
... data processing ...
Intent firstUpdate = new Intent(context, ConfigurationActivity.class);
firstUpdate.setAction("android.appwidget.action.APPWIDGET_ENABLED");
firstUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widget_id);
context.sendBroadcast(firstUpdate);
dialog.dismiss();
setResult(RESULT_OK, firstUpdate);
finish();
} else {
Log.i("Schedule", "Missing selections");
}
}
};
谢谢你的帮助。
最佳答案
多亏了“布拉格纳尼”,我才得以成功。以下是最终代码:
private class ProgressTask extends AsyncTask<String, Void, Boolean>
{
private ProgressDialog dialog;
private ConfigurationActivity activity;
public ProgressTask(ConfigurationActivity activity)
{
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
private Context context;
protected void onPreExecute()
{
dialog = new ProgressDialog(context);
dialog.setMessage("Prosimo počakajte da naloži podatke.");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.show();
}
@Override
protected void onPostExecute(final Boolean success)
{
if (dialog.isShowing())
{
dialog.dismiss();
}
if (success)
{
Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(context, "ERROR", Toast.LENGTH_LONG).show();
}
}
@Override
protected Boolean doInBackground(final String... args)
{
try {
... processing ...
return true;
} catch (Exception e){
Log.e("Schedule", "UpdateSchedule failed", e);
return false;
}
}
}
呼叫班级:
new ProgressTask(ConfigurationActivity.this).execute();
谢谢布拉纳尼!
关于android - Android ProgressDialog未显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16152019/