Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

android - How i can display name of my target in CalendarView?

At the moment I managed to add a date field to my model Target. The structure you can see in the screen:

enter image description here

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

enter image description here

Q: How i can display target name for a specific date?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...