tags:

views:

39

answers:

1

I have implemented the following function to be used with showDialog() and onDialogCreate(), but I would like the method to be called each time because it calculates the text of a text view every time the dialog is displayed.

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;
}

Can anyone help?

+1  A: 

Activities will only call onCreateDialog once and keep references to each dialog. If you want to update a dialog you can override onPrepareDialog(int id, Dialog dialog) which is called every time showDialog is called.

So for your example above where you update the TextView with the score you can do the following:

@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()));
}

You may also want to keep the ScoreCalculator in a member variable so you don't instantiate a new object everytime the dialog is shown.

Frank
Perfect; thanks! And as for the member variable, thanks for the tip, but it's already on my todo list. I just wanted to get the dialog to show the right information. :-)
Fran Fitzpatrick