Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset DatePicker when selecting cancel Button

Tags:

android

I have a datePicker dialog in my app. It works fine when I select a date. However if in the dialog I change the date and then press cancel, the original edittext remains unchanged as it should, but the Date picker still has the date from before when it was cancelled. I would like to make sure that EVERY time I go into the Date Picker, it sets the date from the EditText. My code is as follows.

onCreate() Method

//  Date Listener
    fdEtDate.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            showDialog(DATE_DIALOG_ID);
            return false;
        }
    });

Further down the Activity

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    dateSetListener,
                    cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
    case STD_DIALOG_ID:
        return new TimePickerDialog(this,
                    stdSetListener,
                    stdHH, stdMM,
                    true);
    case STA_DIALOG_ID:
        return new TimePickerDialog(this,
                    staSetListener,
                    staHH, staMM,
                    true);
    }
    return null;
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener dateSetListener =
    new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DATE, dayOfMonth);
            updateDisplay();
        }
};
// updates the date in the TextView
private void updateDisplay() {
    fdEtDate.setText(
        new StringBuilder()
                .append(Utils.pad(cal.get(Calendar.DATE))).append(Const.SQL_DATE_SEP)
                .append(Utils.pad(cal.get(Calendar.MONTH)+1)).append(Const.SQL_DATE_SEP)
                .append(cal.get(Calendar.YEAR)));

    fdEtStd.setText(
        new StringBuilder()
                .append(Utils.pad(stdHH)).append(Const.SQL_TIME_SEP)
                .append(Utils.pad(stdMM)));

    fdEtSta.setText(
        new StringBuilder()
                .append(Utils.pad(staHH)).append(Const.SQL_TIME_SEP)
                .append(Utils.pad(staMM)));
}

Is seems to create a new Dialog each time, so why isn't it initialised each time with the Date from fdEtDate (EditText)

Thanks in advance

like image 837
PeterB Avatar asked Nov 22 '11 12:11

PeterB


People also ask

What is DatePicker dialog?

Android DatePicker is a user interface control that is used to select the date by day, month, and year in the android application.


1 Answers

you should use onPrepareDialog to set the initial date every time the dialog is shown.

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
    case START_DATE_DIALOG_ID:
        ((DatePickerDialog) dialog).updateDate(
            mCalendar.get(Calendar.YEAR),
            mCalendar.get(Calendar.MONTH),
            mCalendar.get(Calendar.DAY_OF_MONTH))
    }
}

A dialog is created only the first time it is shown and from that point it is reused unless you destroy it explicitly with removeDialog().

If you are using onCreateDialog(int) to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should call removeDialog(int). This will remove any internal references to the object and if the dialog is showing, it will dismiss it

http://developer.android.com/guide/topics/ui/dialogs.html

like image 188
Chiara Avatar answered Sep 27 '22 02:09

Chiara