2

I'm new to programming and I'm developing a android app. In there i have two edittext and I need to set different dates using date picker. The problem is when it set the date from datepicker widget, the date values of both edittexts are changed. how can I fix this??? here's my code

 Calendar myCalendar = Calendar.getInstance();
 Calendar myCalendar2 = Calendar.getInstance();

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
    myCalendar.set(Calendar.YEAR, year);
    myCalendar.set(Calendar.MONTH, monthOfYear);
    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

    myCalendar2.set(Calendar.YEAR, year);
    myCalendar2.set(Calendar.MONTH, monthOfYear);
    myCalendar2.set(Calendar.DAY_OF_MONTH, dayOfMonth);

    updateLabel();
    updateLabel2();

}

 private void updateLabel() {
        sDate.setText(sdf.format(myCalendar.getTime()));
        }
 private void updateLabel2() {
        eDate.setText(sdf.format(myCalendar2.getTime()));
        }

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.et_SDate: 
         new DatePickerDialog(Domestic.this,this, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        break;

    case R.id.et_EDate:
        new DatePickerDialog(Domestic.this,this, myCalendar2
                .get(Calendar.YEAR), myCalendar2.get(Calendar.MONTH),
                myCalendar2.get(Calendar.DAY_OF_MONTH)).show();
        break;
    }
}
  • Try this way...http://stackoverflow.com/questions/23356580/datepickerdialog-is-not-displayed-on-click-of-the-button/23356982#23356982 – Rohit Goswami Jul 04 '14 at 04:21
  • @user3758643, May you tell me what you want the second date to be because I can see in your code that you are just getting the same instance of the date and inserting it into different EditText fields. The second date could be easily calculated on the fly. – Dut A. Jul 07 '14 at 08:17
  • You should use two inner class to override `onDateSet()`. One for each Dialog, and not a single one. – Simon Marquis Aug 23 '14 at 10:21

0 Answers0