Question

I have two date pickers which are connected to each other as when I change the date of the first picker the second ones date is also changed automatically , the two pickers have a listener called on date change listener, as follows

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

    TextView birthDatetv;
    DatePicker birthDayDatePicker,periodDatePicker;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  

        setContentView(R.layout.birthdate);

        birthDatetv = (TextView)findViewById(R.id.textViewDateBirth);

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


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

            @Override
            public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
                // TODO Auto-generated method stub
                birthDateCalculations();
            }
        });

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

            @Override
            public void onDateChanged(DatePicker birthDayDatePicker, int currentYear, int currentMonth, int currentDay) {
                // TODO Auto-generated method stub

                periodDateCalculations();

                }
        });

}

As I mentioned before, the pickers are connected, so when I change one picker date the other is also changed so the two listeners for the two date pickers will be invoked,, I just want the listener that is involved with the picker that the user changed its date to be invoke ,, Any idea how to do that ?

Here is the methods that the listeners do if it may help

public void birthDateCalculations ()
{

    Calendar start = Calendar.getInstance();

    int periodYear = periodDatePicker.getYear();
    int periodMonth = periodDatePicker.getMonth();
    int periodDay = periodDatePicker.getDayOfMonth();

    start.set(periodYear, periodMonth, periodDay);
    birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

    Date periodDate = start.getTime();
    int daysToAdd = 280;
    Calendar cal = Calendar.getInstance();

    cal.setTime(periodDate);
    cal.add(Calendar.DAY_OF_MONTH,daysToAdd );
    System.err.println("-----" +cal.getTime());
    int birthYearAfterCalc = cal.getTime().getYear()+1900;
    System.err.println("birthYearAfterCalc-----" + birthYearAfterCalc);
    int birthMonthAfterCalc = cal.getTime().getMonth();
    System.err.println("birthMonthAfterCalc----" + birthMonthAfterCalc);
    int birthDayAfterCalc = cal.getTime().getDate();
    System.err.println("birthDayAfterCalc"+birthDayAfterCalc);

    //user edit period to get birth
    if( periodDay <= currentDay && periodMonth <= currentMonth && periodYear <= currentYear){
    //the program runs normally
    birthDayDatePicker.updateDate(birthYearAfterCalc, birthMonthAfterCalc, birthDayAfterCalc);

        }
        else {


        new AlertDialog.Builder(birthDate.this)

                        .setTitle("Wrong Data Input!")

                        .setMessage("Error in period date input")

                        .setNeutralButton("Ok",

                        new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog,

                        int which) {

                        }

                        }).show();

          periodDatePicker.updateDate(currentYear,currentMonth, currentDay);
            birthDayDatePicker.updateDate(currentYear, currentMonth, currentDay);   


                    }


}

public void periodDateCalculations ()
{
    Calendar start2 = Calendar.getInstance();

    // get the chosen date from birth date picker
    int birthYear = birthDayDatePicker.getYear();
    int birthMonth = birthDayDatePicker.getMonth();
    int birthDay = birthDayDatePicker.getDayOfMonth();

    //set the chosen date to calendar instance

    start2.set(birthYear, birthMonth, birthDay);

    birthDayDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
    periodDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

    //get the date 
    Date birthDate = start2.getTime();
    //int constrain = currentMonth + 9;

    int daysToAdd = -280;

    //another instance from calendar 
    Calendar cal2 = Calendar.getInstance();

    //set birth date chosen to calendar
    cal2.setTime(birthDate);

    //add -280 day to birth day to get period date
    cal2.add(Calendar.DAY_OF_MONTH,daysToAdd);
    System.err.println("-----" +cal2.getTime());
    int periodYearAfterCalc = cal2.getTime().getYear()+1900;
    System.err.println("periodYearAfterCalc-----" + periodYearAfterCalc);
    int periodMonthAfterCalc = cal2.getTime().getMonth();
    System.err.println("periodMonthAfterCalc----" + periodMonthAfterCalc);
    int periodDayAfterCalc = cal2.getTime().getDate();
    System.err.println("periodDayAfterCalc"+periodDayAfterCalc);

    //edit birth date picker from user  
        if(birthDay >= currentDay && birthMonth >= currentMonth && birthYear >= currentYear){
            //the program runs normally
            periodDatePicker.updateDate(periodYearAfterCalc, periodMonthAfterCalc, periodDayAfterCalc);     

            }
            else{


                            new AlertDialog.Builder(birthDate.this)

                            .setTitle("Wrong Data Input!")

                            .setMessage("birth day input error")

                            .setNeutralButton("Ok",

                            new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,

                            int which) {


                            }

                            }).show();

                            birthDayDatePicker.updateDate(currentYear,currentMonth, currentDay);
                            periodDatePicker.updateDate(currentYear, currentMonth, currentDay); 


                        }

    }

}
Was it helpful?

Solution

what you could do is set the corresponding listener to null when the other one takes action, and then after the task, you could turn it back on.

for example:

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

    @Override
    public void onDateChanged(DatePicker periodDatePicker, int currentYear, int currentMonth,int currentDay) {
        // TODO Auto-generated method stub
        birthDayDatePicker.init(currentYear, currentMonth, currentYear, null);
        birthDateCalculations();
        restartBirthDayDatePickerListener(); 
        // ^here you turn back on that init command that we have just set to null.
    }
});

not a glamorous solution, but perhaps you can make heads or tails of it.

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