我如何在CalendarView中显示目标的名称? [英] How i can display name of my target in CalendarView?
问题描述
此刻,我设法将date
字段添加到模型Target
中.
您可以在屏幕上看到的结构:
At the moment I managed to add a date
field to my model Target
.
The structure you can see in the screen:
我还有一个片段在其中显示日历.如何通过单击日期来判断是否有针对该日期创建的目标?
Also i have a fragment where I display the calendar. How can I tell by clicking on a date if there is a target created for that date?
class CalendarFragment : Fragment() {
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)
setupViews()
}
private fun setupViews() {
calendarView.setOnDateChangeListener { view, year, month, dayOfMonth ->
dateView.text = //name of Target
}
}
}
例如,现在我只在日历中显示带有下一个代码的日期:
For example now i display only date in calendar with next code:
dateView.text = "Selected date is " + dayOfMonth + "/" + (month + 1) + "/" + year
问:我如何显示特定日期的目标名称?
UPD: 现在,我将此代码插入片段中,该片段取自 answer :
UPD: Right now i insert this code in my fragment, which i took from this answer:
val uid = FirebaseAuth.getInstance().currentUser!!.uid
val rootRef = FirebaseDatabase.getInstance().reference
val targetsRef = rootRef.child("targets").child("users").child(uid).child("targets")
val query = targetsRef.orderByChild("date")
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.children) {
val name = ds.child("name").getValue(String::class.java)
Log.d("some", name)
}
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("some", databaseError.getMessage()) //Don't ignore errors!
}
}
query.addListenerForSingleValueEvent(valueEventListener)
但是现在当我进入片段时,无论日历中选择的日期如何,我都会获得创建的所有目标的名称.
But now when I go into the fragment I get the names of all the targets created regardless of the selected date in the calendar.
推荐答案
根据前面的注释,您确认当某人单击日历中的日期以查询使用该日期的targets
节点时,您是否想要一个或多个结果.但是第一件事.
According to the earlier comments, you confirmed that you want when someone is clicking on a date in the calendar to query the targets
node using that date to find one or more results. But first thing first.
出于搜索目的,可以将date
存储为字符串,因为您可以使用比较whereEqualTo()
方法,但是如果要订购结果,则由于
For searching purposes, storing the date
as a String might work, since you can use for comparation whereEqualTo()
method but if you want to order the results, it won't work since the strings are order lexicographically. So you might consider storing the date
as a ServerValue.TIMESTAMP
, as explained in my answer from the following post: