1

I'm trying to solve an odd problem. I'm new to Android programming (and this site as a poster), so bear with me for a bit. I have a datePickerDialog that I called from my Main Activity, and the TextView within that class updates the year,month, and date correctly, but for some reason when I use getters to get the year,month, and date in my Main Activity, it just returns 0. Am I doing something wrong by using the getters?

// Activity Method

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
    firstDatem = ((DatePickerFragment) newFragment).getMonth();
    firstDated = ((DatePickerFragment) newFragment).getDay();
    firstDatey = ((DatePickerFragment) newFragment).getYear();
}

// DatePickerDialog

private int year;
private int month;
private int day;


public void onDateSet(DatePicker view, int year, int month, int day) {
    // do some stuff for example write on log and update TextField on activity
    String monthstring;
    switch ( month ) {
        case 0: monthstring = "January";
                break;
        case 1: monthstring = "February";
                break;
        case 2: monthstring = "March";
                break;
        case 3: monthstring = "April";
                break;
        case 4: monthstring = "May";
                break;
        case 5: monthstring = "June";
                break;
        case 6: monthstring = "July";
                break;
        case 7: monthstring = "August";
                break;
        case 8: monthstring = "September";
                break;
        case 9: monthstring = "October";
                break;
        case 10: monthstring = "November";
                break;
        case 11: monthstring = "December";
                break;
        default:
                monthstring = "";
                break;
    }
    ((TextView) getActivity().findViewById(R.id.date_text)).setText( "First date set to " + monthstring + " " + day + ", "+ year); 
}
public int getYear()
{
    return year;
}
public int getMonth()
{
    return month;
}
public int getDay()
{
    return day;
}
}
  • http://stackoverflow.com/questions/18211684/how-to-transfer-the-formatted-date-string-from-my-datepickerfragment. You can use a interface as a callback to the activity – Raghunandan Jul 08 '14 at 03:56

1 Answers1

0

You should have your DatePickerDialog call back to your MainActivity. It will make things much easier and cleaner. Here is an example of how this pattern works along with some comments:

Create a DatePickerDialogFragment like this:

public class DatePickerDialogFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
    // This interface is implemented by this fragment's container activity.
    // In your case, it will be implemented by MainActivity.
    // When the container activity creates an instance of this fragment, it passes in a reference to this
    // method.  This allows the current fragment to use that reference to pass the entered date values back 
    // to that container activity as soon as they are entered by the user.
    public static interface DatePickedListener
    {
        public void onDatePicked(int selectedYear, int selectedMonth, int selectedDay);
    }

    private DatePickedListener listener;


    @Override
    public void onAttach(Activity activity)
    {
        // when the fragment is initially attached to the activity, 
        // cast the activity to the callback interface type
        super.onAttach(activity);

        try
        {
            listener = (DatePickedListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + 
                " must implement " + DatePickedListener.class.getName());
        }
    }


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {

        // the activity passes in these arguments when it 
        // creates the dialog. use them to create the dialog
        // with these initial values set
        Bundle b = getArguments();

        int year = b.getInt("set_year");
        int month = b.getInt("set_month");
        int day = b.getInt("set_day");

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }


    @Override
    public void onDateSet(DatePicker view, int setYear, int setMonth, int setDay)
    {
        // when the date is selected, immediately send it to the activity via 
        // its callback interface method

        listener.onDatePicked(setYear, setMonth, setDay);
    }

}

Have your MainActivity implement the DatePickerDialogFragment.DatePickedListener interface, as defined in DatePickerDialogFragment above. Then in your MainActivity, when the user presses the button to display the date picker, have something like this in the event handler:

Calendar cal = Calendar.getInstance();

Bundle b = new Bundle();  // create a bundle object to pass currently set date to DatePickerDialogFragment

b.putInt("set_year", cal.get(Calendar.YEAR));  
b.putInt("set_month", cal.get(Calendar.MONTH)); 
b.putInt("set_day", cal.get(Calendar.DAY_OF_MONTH)); 

// show the date picker fragment (which contains a date picker dialog)
DialogFragment datePickerDialogFragment = new DatePickerDialogFragment();
datePickerDialogFragment.setArguments(b);  // set the bundle on the DatePickerDialogFragment        
datePickerDialogFragment.show(getSupportFragmentManager(), TAG_DATE_PICKER_FRAGMENT);   

When the user picks a date, onDateSet will be called in your date picker fragment, and that will call listener.onDatePicked(setYear, setMonth, setDay), which calls onDatePicked() in your MainActivity. So there is no need for your MainActivity to use any kind of getter methods to retrieve the results - the results are returned automatically when the user finishes the date dialog.

JDJ
  • 4,298
  • 3
  • 25
  • 44
  • I have another method that I omitted that got the dates from the Calendar. – user3814742 Jul 08 '14 at 04:00
  • It looks almost identical to the onCreateDialog method on Google's picker section: http://developer.android.com/guide/topics/ui/controls/pickers.html – user3814742 Jul 08 '14 at 04:01