日期选择器于 1970 年 1 月 1 日重置
Date picker is reseted on 1 Jan 1970
我的 firebase 实时数据库中有字段 date
。
现在例如,如果我输入片段并且不更改日期,日期更改为:1 Jan 1970
即使之前已安装。
这是我的片段代码:
class TargetEditFragment : BaseFragment(), View.OnClickListener, TargetEditContract {
private val presenter = TargetEditPresenter(contract = this)
private val safeArgs: TargetEditFragmentArgs by navArgs()
private val targetGuid: String
get() = safeArgs.guid
private var parsedDate: LocalDate? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter.setInitialData(targetGuid)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_target_add, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.fetchTarget()
setupViews()
}
override fun updateViewsContent(
name: String?,
date: String?
) {
nameEditText?.text = Editable.Factory.getInstance().newEditable(name)
dateView?.text = Editable.Factory.getInstance().newEditable(date)
}
override fun onClick(v: View?) {
presenter.onViewClick(v?.id ?: return, targetGuid)
}
override fun editTarget(targetGuid: String) {
val name = nameEditText?.text.toString().trim()
val date = parsedDate?.atStartOfDay()?.toInstant(ZoneOffset.UTC)?.toEpochMilli() ?: 0L
if (targetGuid.isEmpty()) presenter.addTarget(name, date)
else presenter.updateTarget(name, date)
}
override fun deleteTarget() {
presenter.deleteTarget()
}
override fun closeView() {
findNavController().navigate(R.id.show_list)
}
override fun showWarningDialog() {
activity?.showWarningDialog(description = getString(R.string.warning_description))
}
private fun setupViews() {
showDatePickerDialog()
addActionView?.setOnClickListener(this)
deleteActionView?.setOnClickListener(this)
}
private fun showDatePickerDialog() {
val date = LocalDate.now(ZoneId.systemDefault())
val currentYear = date.year
val currentMonth = date.monthValue
val currentDay = date.dayOfMonth
val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
pickDate.setOnClickListener {
val datePickDialog = DatePickerDialog(
activity,
R.style.DatePickerDialogTheme,
DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
val selectedDate = LocalDate.of(year, month + 1, dayOfMonth)
val dateString = selectedDate.format(dateFormatter)
dateView.text = dateString
parsedDate = LocalDate.parse(dateString, dateFormatter)
},
currentYear,
currentMonth - 1,
currentDay
)
datePickDialog.show()
datePickDialog.setOnCancelListener { dialog -> dialog.dismiss() }
}
}
}
更新:
我看到那是因为我把 elvis 0L
。但如何修复正确?
如果我不打电话给 showDatePickerDialog
,我的 parsedDate
将永远是 null
。
但有趣的是 editTarget
我需要保存日期。
I see that is because i put elvis 0L. But how it fix correct?
If i don't call showDatePickerDialog my parsedDate always will be null. But in fun editTarget i need get saved date.
没错。如果您希望回退日期不是 1970 年 1 月 1 日,那么您需要将 parsedDate
设置为它应该是的任何值。你说它在 Firebase 实时数据库中,但我没有看到你在任何地方引用该数据库来获取值,以便可以适当地设置它。
我的建议是让演示者调用您的数据层以侦听相关实时数据库值的更新,并在您的片段中有一个回调,每当数据库值更新时都会更新 parsedDate
.
我的 firebase 实时数据库中有字段 date
。
现在例如,如果我输入片段并且不更改日期,日期更改为:1 Jan 1970
即使之前已安装。
这是我的片段代码:
class TargetEditFragment : BaseFragment(), View.OnClickListener, TargetEditContract {
private val presenter = TargetEditPresenter(contract = this)
private val safeArgs: TargetEditFragmentArgs by navArgs()
private val targetGuid: String
get() = safeArgs.guid
private var parsedDate: LocalDate? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
presenter.setInitialData(targetGuid)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_target_add, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
presenter.fetchTarget()
setupViews()
}
override fun updateViewsContent(
name: String?,
date: String?
) {
nameEditText?.text = Editable.Factory.getInstance().newEditable(name)
dateView?.text = Editable.Factory.getInstance().newEditable(date)
}
override fun onClick(v: View?) {
presenter.onViewClick(v?.id ?: return, targetGuid)
}
override fun editTarget(targetGuid: String) {
val name = nameEditText?.text.toString().trim()
val date = parsedDate?.atStartOfDay()?.toInstant(ZoneOffset.UTC)?.toEpochMilli() ?: 0L
if (targetGuid.isEmpty()) presenter.addTarget(name, date)
else presenter.updateTarget(name, date)
}
override fun deleteTarget() {
presenter.deleteTarget()
}
override fun closeView() {
findNavController().navigate(R.id.show_list)
}
override fun showWarningDialog() {
activity?.showWarningDialog(description = getString(R.string.warning_description))
}
private fun setupViews() {
showDatePickerDialog()
addActionView?.setOnClickListener(this)
deleteActionView?.setOnClickListener(this)
}
private fun showDatePickerDialog() {
val date = LocalDate.now(ZoneId.systemDefault())
val currentYear = date.year
val currentMonth = date.monthValue
val currentDay = date.dayOfMonth
val dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
pickDate.setOnClickListener {
val datePickDialog = DatePickerDialog(
activity,
R.style.DatePickerDialogTheme,
DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
val selectedDate = LocalDate.of(year, month + 1, dayOfMonth)
val dateString = selectedDate.format(dateFormatter)
dateView.text = dateString
parsedDate = LocalDate.parse(dateString, dateFormatter)
},
currentYear,
currentMonth - 1,
currentDay
)
datePickDialog.show()
datePickDialog.setOnCancelListener { dialog -> dialog.dismiss() }
}
}
}
更新:
我看到那是因为我把 elvis 0L
。但如何修复正确?
如果我不打电话给 showDatePickerDialog
,我的 parsedDate
将永远是 null
。
但有趣的是 editTarget
我需要保存日期。
I see that is because i put elvis 0L. But how it fix correct?
If i don't call showDatePickerDialog my parsedDate always will be null. But in fun editTarget i need get saved date.
没错。如果您希望回退日期不是 1970 年 1 月 1 日,那么您需要将 parsedDate
设置为它应该是的任何值。你说它在 Firebase 实时数据库中,但我没有看到你在任何地方引用该数据库来获取值,以便可以适当地设置它。
我的建议是让演示者调用您的数据层以侦听相关实时数据库值的更新,并在您的片段中有一个回调,每当数据库值更新时都会更新 parsedDate
.