我想使用DateDialogFragment并把我到达那里的日期到另一个片段
如何在获得日历实例后放置设置的日期?
class DateDialogFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
private val calendar = Calendar.getInstance()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)
return DatePickerDialog(requireActivity(), this, year, month, dayOfMonth)
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
calendar.set(Calendar.YEAR, year)
calendar.set(Calendar.MONTH, month)
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth)
val selectedDate = calendar.time
val selectedDateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.JAPAN).format(calendar.time)
val bundle = Bundle()
bundle.putString("SELECTED_DATE", selectedDateFormat)
bundle.putParcelable("SELECTED_DATE_PARCELABLE", selectedDate)
setFragmentResult("REQUEST_KEY", bundle)
}
}
我发现了一个叫parcelable的东西,但是我不知道怎么用!
我尝试使用Long类型来处理日期,但是数据库无法处理数据比较,所以我尝试使用SQLite的Parcelable。
你可以在片段之间传递数据作为参数:https://developer.android.com/guide/navigation/navigation-pass-data?authuser=1
或其他选项:如何在Fragments之间传递值