如何将DialogFragment中的日期保存到数据库中

时间:2017-09-17 13:04:53

标签: java android datepicker

我正在使用代码从Android教程创建DatePickerFragment。在我选择日期之后,我想将其与其他变量一起保存到数据库中。我该怎么做?

我的主要课程:(顺便说一句。如果可能,我更愿意提取DatePickerFragment类)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    database = new DatabaseHelper(this);
    Button button = (Button) findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DialogFragment fragment = new DatePickerFragment();
            fragment.show(getSupportFragmentManager(), "datePicker");
        }
    });
}

 public static class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            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);

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

        public void onDateSet(DatePicker view, int year, int month, int day) {
            database.insert(dateInserted, otherVariable);
        }
    }

0 个答案:

没有答案