2

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);
    }
}
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Why don't you move the `onDateSet()` code from the main Actvity to your DatePickerFragment? – Sam Oct 17 '12 at 20:22
  • @Sam Since you beat me to it, go ahead and put that as the answer. He's set his listener to be `this`, which is the `Fragment` object, while his code sits in the `Activity`. – Cat Oct 17 '12 at 20:25

1 Answers1

1

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);
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks, It works now but I'm trying to make it go to a different activity for each page, i used an if statement but I guess I'm doing it wrong. – user1741292 Oct 17 '12 at 21:58
  • $ public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { if (month==1) { Activity activity = getActivity(); Intent intent = new Intent(activity, January_1.class); startActivity(intent); } } $ – user1741292 Oct 17 '12 at 21:59
  • Oh, I see how you said to do it, missed the end of your post. But when I type your intent and switch in it says activity cannot be resolved into a variable and wants me to change activity to something different every time it is typed in the code – user1741292 Oct 17 '12 at 22:13
  • It wasn't there at first, I just updated my post after your comments. :) If this answer works for you, please click the checkmark. – Sam Oct 17 '12 at 22:17
  • Ok I have the switch working but how do I specify a certain day? – user1741292 Oct 17 '12 at 22:28
  • Could you be more specific? Are you interested in just the date (1st, 2nd, etc) or the day of the week (Monday, Tuesday, ...) or a full date (November 5th, 1955)? – Sam Oct 17 '12 at 22:48
  • just the day. I need to know how to get the month, then the day of that month that was entered to go to a certain screen, of which there will be one per day of the year – user1741292 Oct 17 '12 at 23:27
  • I see, before you create _366_ (leap years included) Activities, I suggest designing a generic Activity and using `Intent#putExtra()` to pass the year, month, and day. (Read [this question](http://stackoverflow.com/q/2965109/1267661)) – Sam Oct 17 '12 at 23:46