How I Can Display Name Of My Target In Calendarview?
Solution 1:
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.
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:
Furthermore, I will provide you the solution for your particular use, where the date
is a String but can simply change it later if you'll consider using the solution above. So I will use as a selected date the following String:
valselectedDate="24 October 2019"
Assuming that this value is coming from the selection of the date from your calendar, please try the code below:
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").equalTo(selectedDate)
val valueEventListener = object : ValueEventListener {
overridefunonDataChange(dataSnapshot: DataSnapshot) {
for (ds in dataSnapshot.children) {
val name = ds.child("name").getValue(String::class.java)
Log.d(TAG, name)
}
}
overridefunonCancelled(databaseError: DatabaseError) {
Log.d(TAG, databaseError.getMessage()) //Don't ignore errors!
}
}
query.addListenerForSingleValueEvent(valueEventListener)
In this case, the result in your logcat will be:
target
Edit:
I get the names of all the targets created regardless of the selected date in the calendar.
You get all the dates because you forgot to call .equalTo(selectedDate)
. So please change:
valquery= targetsRef.orderByChild("date")
to
valquery= targetsRef.orderByChild("date").equalTo(selectedDate)
Post a Comment for "How I Can Display Name Of My Target In Calendarview?"