如何在Firebase实时数据库中保存LocalData?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Firebase实时数据库中保存LocalData?相关的知识,希望对你有一定的参考价值。

我对使用Kotlin和编程非常陌生,目前正在制作带有事件的日历。我想将这些事件连接到Firebase时遇到问题。

我使用的是我在git(https://github.com/kizitonwose/CalendarView)中找到的示例,该示例使用ThreeTen库作为日期。这是事件对象:

class Event (val id: String, val text: String, val date: LocalDate) : Serializable

数据变量为LocalData类型,这是导致我出现问题的原因,因为Firebase似乎只接受String,Int等类型的变量...

我试图通过toString和Gson()将变量传递给String,但没有成功。

这里是代码,如果有帮助的话

 private val inputDialog by lazy {
    val editText = AppCompatEditText(requireContext())
    val layout = FrameLayout(requireContext()).apply {
        // Setting the padding on the EditText only pads the input area
        // not the entire EditText so we wrap it in a FrameLayout.
        setPadding(20, 20, 20, 20)
        addView(editText, FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT))
    }
    AlertDialog.Builder(requireContext())
        .setTitle(getString(R.string.example_3_input_dialog_title))
        .setView(layout)
        .setPositiveButton(R.string.save) { _, _ ->
            saveEvent(editText.text.toString())
            // Prepare EditText for reuse.
            editText.setText("")
        }
        .setNegativeButton(R.string.close, null)
        .create()
        .apply {
            setOnShowListener {
                // Show the keyboard
                editText.requestFocus()
                context.inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
            }
            setOnDismissListener {
                // Hide the keyboard
                context.inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
            }
        }
}
private var selectedDate: LocalDate? = null
private val today = LocalDate.now()
private val titleSameYearFormatter = DateTimeFormatter.ofPattern("MMMM")
private val titleFormatter = DateTimeFormatter.ofPattern("MMM yyyy")
private val selectionFormatter = DateTimeFormatter.ofPattern("yyyy MM dd")
private val events = mutableMapOf<LocalDate, List<Event>>()
private var prueba = Gson().toJson(events)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_calendar, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    mDatabaseReference = mDatabase!!.reference.child("events")
    exThreeRv.layoutManager = LinearLayoutManager(requireContext(), RecyclerView.VERTICAL, false)
    exThreeRv.adapter = eventsAdapter
    exThreeRv.addItemDecoration(DividerItemDecoration(requireContext(), RecyclerView.VERTICAL))
    val daysOfWeek = daysOfWeekFromLocale()
    val currentMonth = YearMonth.now()
    exThreeCalendar.setup(currentMonth.minusMonths(10), currentMonth.plusMonths(10), daysOfWeek.first())
    exThreeCalendar.scrollToMonth(currentMonth)
    if (savedInstanceState == null) {
        exThreeCalendar.post {
            // Show today's events initially.
            selectDate(today)
        }
    }
    class DayViewContainer(view: View) : ViewContainer(view) {
        lateinit var day: CalendarDay // Will be set when this container is bound.
        val textView = view.exThreeDayText
        val dotView = view.exThreeDotView
        init {
            view.setOnClickListener {
                if (day.owner == DayOwner.THIS_MONTH) {
                    selectDate(day.date)
                }
            }
        }
    }
    exThreeCalendar.dayBinder = object : DayBinder<DayViewContainer> {
        override fun create(view: View) = DayViewContainer(view)
        override fun bind(container: DayViewContainer, day: CalendarDay) {
            container.day = day
            val textView = container.textView
            val dotView = container.dotView
            textView.text = day.date.dayOfMonth.toString()
            if (day.owner == DayOwner.THIS_MONTH) {
                textView.makeVisible()
                when (day.date) {
                    today -> {
                        textView.setTextColorRes(R.color.white)
                        textView.setBackgroundResource(R.drawable.today_bg)
                        dotView.makeInVisible()
                    }
                    selectedDate -> {
                        textView.setTextColorRes(R.color.white)
                        textView.setBackgroundResource(R.drawable.selected_bg)
                        dotView.makeInVisible()
                    }
                    else -> {
                        textView.setTextColorRes(R.color.black)
                        textView.background = null
                        dotView.isVisible = events[day.date].orEmpty().isNotEmpty()
                    }
                }
            } else {
                textView.makeInVisible()
                dotView.makeInVisible()
            }
        }
    }
    exThreeCalendar.monthScrollListener = {
        requireActivity().home.text = if (it.year == today.year) {
            titleSameYearFormatter.format(it.yearMonth)
        } else {
            titleFormatter.format(it.yearMonth)
        }
        // Select the first day of the month when
        // we scroll to a new month.
        selectDate(it.yearMonth.atDay(1))
    }
    class MonthViewContainer(view: View) : ViewContainer(view) {
        val legendLayout = view.legendLayout
    }
    exThreeCalendar.monthHeaderBinder = object : MonthHeaderFooterBinder<MonthViewContainer> {
        override fun create(view: View) = MonthViewContainer(view)
        override fun bind(container: MonthViewContainer, month: CalendarMonth) {
            // Setup each header day text if we have not done that already.
            if (container.legendLayout.tag == null) {
                container.legendLayout.tag = month.yearMonth
                container.legendLayout.children.map { it as TextView }.forEachIndexed { index, tv ->
                    tv.text = daysOfWeek[index].name.first().toString()
                    tv.setTextColorRes(R.color.black)
                }
            }
        }
    }
    exThreeAddButton.setOnClickListener {
        inputDialog.show()
    }
}
private fun selectDate(date: LocalDate) {
    if (selectedDate != date) {
        val oldDate = selectedDate
        selectedDate = date
        oldDate?.let { exThreeCalendar.notifyDateChanged(it) }
        exThreeCalendar.notifyDateChanged(date)
        updateAdapterForDate(date)
    }
}
private fun saveEvent(text: String) {
    if (text.isBlank()) {
        Toast.makeText(requireContext(),
            R.string.example_3_empty_input_text, Toast.LENGTH_LONG).show()
    } else {
        selectedDate?.let {
            events[it] = events[it].orEmpty().plus(
                Event(
                    UUID.randomUUID().toString(),
                    text,
                    it
                )
            )
            uploadFirebase()
            updateAdapterForDate(it)
        }
    }
}
private fun deleteEvent(event: Event) {
    val date = event.date
    events[date] = events[date].orEmpty().minus(event)
    updateAdapterForDate(date)
}
private fun updateAdapterForDate(date: LocalDate) {
    eventsAdapter.events.clear()
    eventsAdapter.events.addAll(events[date].orEmpty())
    eventsAdapter.notifyDataSetChanged()
    exThreeSelectedDateText.text = selectionFormatter.format(date)
}

fun uploadFirebase(){
    val newEvent = mDatabaseReference.push()
    newEvent.setValue(events)
}


override fun onStart() {
    super.onStart()
}
override fun onStop() {
    super.onStop()
}

}

答案

您无法在Firebase Realtime数据库中添加类型为LocalDate的属性,因为它不是supported data-type。但是,有两种方法可以解决此问题:

  1. 您将日期另存为ServerValue.TIMESTAMP,这基本上意味着您保存了自Unix时代以来经过的秒数。在这种情况下,服务器将当前日期写入数据库中。为此,请从以下帖子中查看我的答案:

  2. 您为日期字段指定自定义long值。在这种情况下,由您决定写什么日期。

不幸的是,您无法将这两个选项结合使用,可以使用其中一个。

[谈论LocalDate时,我们通常谈论的是偏移量,在这种情况下,这就是我要做的。我将存储一个Timestamp属性,如第一点所述,它将使服务器填充服务器Timestamp以及一个offset属性,该属性应以天/小时为单位填充偏移量。

以上是关于如何在Firebase实时数据库中保存LocalData?的主要内容,如果未能解决你的问题,请参考以下文章

如何在Firebase实时数据库中保存LocalData?

向 Firebase 实时数据库添加新值时如何保存当前日期/时间

在 Firebase 实时数据库中保存数据 Android Studio Kotlin

如何保存多个谷歌地图标记,它们在 Firebase 实时数据库中的位置,并在打开应用程序时再次显示它们

如何使用 Firebase 实时数据库创建特定用户所有已保存帖子的列表?

如何在 Firebase 实时数据库中搜索类似数组的数据?