user1741292
user1741292

Reputation: 39

DatePicker change view after date is picked

I have a button which pulls up the DatePicker fragment but I can't figure out how to make the view change depending on the date picked. I put onDateSet method in my main.java which is the activity calling the fragment, with intents to change view inside this method, but It does nothing when the date is changed and I'm not sure what I am doing wrong.

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
    }

and my MainScreen.java is

public class MainScreen extends FragmentActivity implements DatePickerDialog.OnDateSetListener {

    static final int DATE_DIALOG_ID = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
    }

    public void showDatePickerDialog(View v) {
        DatePickerFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        Intent intent = new Intent(this, January_1.class);
        startActivity(intent);
    }
}

Upvotes: 2

Views: 1502

Answers (1)

Sam
Sam

Reputation: 86948

You should use the onDateSet() listener in your DatePickerFragement:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Activity activity = getActivity();
        Intent intent = new Intent(activity, January_1.class);
        activity.startActivity(intent);
    }
}

Added from comments

You are using the wrong index for monthOfYear, from the documentation:

monthOfYear The month that was set (0-11) for compatibility with Calendar.

So January is 0, February is 1, etc...
To limit any confusion, I suggest using the month names from the Calendar class and a switch statement:

Intent intent;
switch(monthOfYear) {
case Calendar.JANUARY:
    intent = new Intent(activity, January_1.class);
    break;
case Calendar.FEBRUARY:
    // etc...
}
activity.startActivity(intent);

Upvotes: 1

Related Questions