richardrun
richardrun

Reputation: 253

DatePickerDialog cannot get in onDateSet

Here is the code. I have a Log.e to check if the program go into the onDateSet function, and found that it doesn't.

Here is the code:

private void datePicker(){
    //Calendar datePickercal = Calendar.getInstance();
    DatePickerDialog dialog = new DatePickerDialog(ContentInput.this, new DatePickerDialog.OnDateSetListener(){
        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            Log.e(ErrorTag, "date picker: " + year + " " + month + " " + dayOfMonth);
            cal.set(year,month+1,dayOfMonth);
        }
    }
    ,cal.get(Calendar.YEAR)
    ,cal.get(Calendar.MONTH)
    ,cal.get(Calendar.DAY_OF_MONTH));

    dialog.show();
    dialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                timePicker();
            }
        }
    });
}

Upvotes: 1

Views: 638

Answers (2)

Mike M.
Mike M.

Reputation: 39191

A DatePickerDialog invokes its OnDateSetListener callback in the positive button click. In fact, DatePickerDialog sets up that callback with its own call to setButton(BUTTON_POSITIVE, ...). Your call to that method is overriding that, so the OnDateSetListener is never called.

Move your timePicker() call to the onDateSet() method, and remove the dialog.setButton() call.

Upvotes: 2

gStephin
gStephin

Reputation: 344

here is the code for date picker

 calendar1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            dateflag = "from";
            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");

        }
    });


public static class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar calendar = Calendar.getInstance();
        int yy = calendar.get(Calendar.YEAR);
        int mm = calendar.get(Calendar.MONTH);
        int dd = calendar.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, 2017, 01, 01);
    }

    public void onDateSet(DatePicker view, int yy, int mm, int dd) {
        populateSetDate(yy, mm + 1, dd);
    }

    public void populateSetDate(int year, int month, int day) {
        if (dateflag.equals("from")) {
            from_date_ext.setText(day + "/" + month + "/" + year);
        } else {
            to_date_ext.setText(day + "/" + month + "/" + year);

        }
    }

}

Upvotes: 0

Related Questions