Question

I have an activity in which I have two date pickers, at first I initialise them by those statements

    public class birthDate extends Activity{

Calendar c = Calendar.getInstance();
    int currentYear = c.get(Calendar.YEAR); 
    int currentMonth = c.get(Calendar.MONTH);
    int currentDay = c.get(Calendar.DAY_OF_MONTH);
  public void onCreate(Bundle savedInstanceState) 
{

 super.onCreate(savedInstanceState);  

 setContentView(R.layout.birthdate);

    DatePicker birthDayDatePicker,periodDatePicker;
            birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
            periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);
             periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
            {

                @Override
                public void onDateChanged(DatePicker arg0, int arg1, int arg2,int arg3) {
                    // TODO Auto-generated method stub
                    birthDateCalculations();
                }
            });

            birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener() {

                @Override
                public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) {
                    // TODO Auto-generated method stub

                    periodDateCalculations();

                    }
            });

    }}

I realised that those listeners only happened once, as if I clicked again on the date picker,, the statements won't be applied .. I want a way to have a listener that is active every time I click on the date picker.

Thanks in advance !

Was it helpful?

Solution 2

 birthDayDatePicker = (DatePicker)findViewById(R.id.DatePickerBirthDay);
        periodDatePicker = (DatePicker)findViewById(R.id.DatePickerPeriod);

        //initialize period date picker with current date and on date change listener 
        periodDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener()
        {

            @Override
            public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
                // TODO Auto-generated method stub
                Number = 1;
                birthDayDatePicker.init(currentYear, currentMonth, currentDay, null);
                birthDateCalculations();

            }
        });

        //initialize birth date picker with current date and on date change listener 
        birthDayDatePicker.init(currentYear, currentMonth, currentDay, new OnDateChangedListener () {

            @Override
            public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
                // TODO Auto-generated method stub
                Number = 2;
                periodDatePicker.init(currentYear, currentMonth, currentDay, null);
                periodDateCalculations();

                }
        });

}

> Blockquote

OTHER TIPS

Please initialize the pickers in the oncreate method of the activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top