datePickerDialog Fragment,在Main Activity中获取变量

时间:2014-07-08 03:55:22

标签: java android android-activity android-fragments android-datepicker

我正试图解决一个奇怪的问题。我是Android编程新手(这个网站作为海报),所以请耐心等待一下。我有一个datePickerDialog,我从我的主活动调用,该类中的TextView正确更新年,月和日期,但出于某种原因,当我使用getter获取我的主活动中的年,月和日期时,它只返回0.我使用getter做错了吗?

// 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;
}
}

1 个答案:

答案 0 :(得分:0)

您应该将DatePickerDialog回调给MainActivity。它将使事情变得更容易和更清洁。以下是此模式如何与一些注释一起使用的示例:

像这样创建一个DatePickerDialogFragment:

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

}

让MainActivity实现DatePickerDialogFragment.DatePickedListener接口,如定义的那样         在上面的DatePickerDialogFragment中。然后在您的MainActivity中,当用户按下按钮进行显示时         日期选择器,在事件处理程序中有这样的东西:

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

当用户选择日期时,将在日期选择器片段中调用onDateSet         将调用listener.onDatePicked(setYear,setMonth,setDay),它会调用你的onDatePicked()         主要活动。因此,您的MainActivity不需要使用任何类型的getter方法         检索结果 - 当用户完成日期时自动返回结果         对话框。