DatepickerDialog is not showing dates properly

465 views Asked by At

I am using two datePickerDialogs (Say x and y) to set two different dates. I am setting different max and min dates using two separate Calendar instances (Say xCal and yCal). xCal and yCal are static variables. Also two separate onDateSetListeners (Say xListener and yListener).

When I click x datePickerDialog, it shows all the settings (max and min dates) correctly. However, when I click y datePickerDialog it shows the settings of x datePickerDialog and vice versa.

Here is the sample code present:

protected Dialog onCreateDialog(int id) {
        if (id == 999) {
            DateFormat inputDF  = new SimpleDateFormat(AppConstants.DATE_FORMAT_MM_DD_YYYY_2);
            Date date1 = null;
            if(dateFlag.equalsIgnoreCase(flag1)){
                int lmYear, lmMonth, lmDay;
                try {

                    date1 = inputDF.parse(DateUtils.formatDate(
                            date, AppConstants.DATE_FORMAT_YYYY_MM_DD,
                            AppConstants.DATE_FORMAT_MM_DD_YYYY_2));

                            lCalendar.setTime(date1);
                            lmMonth = lCalendar.get(Calendar.MONTH);
                            lmDay = lCalendar.get(Calendar.DAY_OF_MONTH);
                            lmYear = lCalendar.get(Calendar.YEAR);

                    DatePickerDialog lDialog = new DatePickerDialog(this, lDateListener, lmYear, lmMonth, lmDay);
                    DatePicker lDatePicker = lDialog.getDatePicker();
                    Calendar calendar = Calendar.getInstance();
                    lDatePicker.setMaxDate(calendar.getTimeInMillis());
                    lDatePicker.setMinDate(DateUtils.getLMDMinDate());
                    return lDialog;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }else if(dateFlag.equalsIgnoreCase(flag2)) {
                int ddYear, ddMonth, ddDay;
                if (userSession.get(date)!=null){
                    try {
                        date1 = inputDF.parse(userSession.get(date));
                        dCalendar.setTime(date1);
                        ddMonth = dCalendar.get(Calendar.MONTH);
                        ddDay = dCalendar.get(Calendar.DAY_OF_MONTH);
                        ddYear = dCalendar.get(Calendar.YEAR);

                        DatePickerDialog dDialog = new DatePickerDialog(this, dDateListener, ddYear, ddMonth, ddDay);
                        DatePicker dDatePicker = dDialog.getDatePicker();

                        dDatePicker .setMaxDate(DateUtils.getMaxDate(dCalendar));
                        dDatePicker .setMinDate(DateUtils.getMinDate(dCalendar));
                        return dDialog;
                    } catch (ParseException e) {
                        e.printStackTrace();

                        ddMonth = dCalendar.get(Calendar.MONTH);
                        ddDay = dCalendar.get(Calendar.DAY_OF_MONTH);
                        ddYear = dCalendar.get(Calendar.YEAR);

                        DatePickerDialog dDialogNew = new DatePickerDialog(this, dDateListener,ddYear, ddMonth, ddDay);
                        DatePicker dDatePicker = dDialogNew.getDatePicker();
                        dDatePicker .setMaxDate(DateUtils.getMaxDate(dCalendar));
                        dDatePicker .setMinDate(DateUtils.getMinDate(dCalendar));
                        return dDialogNew;
                    }

                } else {
                    ddMonth = dCalendar.get(Calendar.MONTH);
                    ddDay = dCalendar.get(Calendar.DAY_OF_MONTH);
                    ddYear = dCalendar.get(Calendar.YEAR);

                    DatePickerDialog dpDialog = new DatePickerDialog(this, dDateListener,ddYear, ddMonth, ddDay);
                    DatePicker datePicker = dpDialog.getDatePicker();
                    datePicker.setMaxDate(DateUtils.getMaxDate(dCalendar));
                    datePicker.setMinDate(DateUtils.getMinDate(dCalendar));
                    return dpDialog;
                }
            }
        }
        return null;
    }

Update: Following is the code snippet of onClickListener, the same method is called when I click any of the button :

public void setDate(View view) {
        if(view.getId()==R.id.date1_textView){
            dateFlag = flag1;
        }else {
            dateFlag = flag2;
        }
        showDialog(999);
    }
0

There are 0 answers