我已经实现了以下与showDialog()和onDialogCreate()一起使用的函数,但是我希望每次调用该方法,因为每次显示对话框时它都会计算文本视图的文本。
private AlertDialog overallScoreDialog(){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.overall_score_dialog,
(ViewGroup) findViewById(R.id.overall_score_dialog_layout_root));
alert.setTitle("Results");
TextView tv = (TextView) layout.findViewById(R.id.overallscoreresults);
ScoreCalculator sc = new ScoreCalculator(this, calculatorVO);
tv.setText(Double.toString(sc.getTotalScore()));
alert.setView(layout);
alert.setPositiveButton(R.string.done, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
return;
}
});
AlertDialog ad = alert.create();
return ad;
}
有人可以帮忙吗?
答案 0 :(得分:1)
活动只会调用onCreateDialog一次并保持对每个对话框的引用。如果要更新对话框,可以覆盖每次调用showDialog时调用的onPrepareDialog(int id,Dialog对话框)。
因此,对于上面的示例,您使用分数更新TextView,您可以执行以下操作:
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
TextView tv = (TextView) dialog.findViewById(R.id.overallscoreresults);
ScoreCalculator sc = new ScoreCalculator(this, calculatorVO);
tv.setText(Double.toString(sc.getTotalScore()));
}
您可能还希望将ScoreCalculator保留在成员变量中,这样每次显示对话框时都不会实例化新对象。