現在kotlinの学習を始め、自作のCalendarViewを使用した簡単なカレンダーアプリを作成しています。
やりたいことについては、カレンダーの日にちにイベント(本アプリではアイコンを付与)を付与し、月毎にそのイベント数を取得したいのですが、イベント数の取得がうまくいかない状況です。
月を指定して、その月のイベント数を取得する方法を教えていただきたいのですが、何か方法はありますでしょうか。(例えば、1月10,13,15日にイベントがあった場合、イベント数3を取得したい。)
学習を始めたばかりなので、何かアドバイスをいただけると嬉しいです。
実装しているコードは↓になります。
class NotificationsFragment : Fragment() { companion object { private const val TABLE_NAME = "memos" } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_notifications, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val calendarView = view.findViewById<View>(R.id.calendarView) as CalendarView /*DBに保存している日付情報を取得*/ val helper = DBHelper(requireContext()) helper.readableDatabase.use { db -> db.query( TABLE_NAME, arrayOf("id", "date"), null, null, null, null, null, null) .use { cursor -> if (cursor.moveToFirst()) { /*イベントを追加する箱を用意*/ val events: ArrayList<EventDay> = ArrayList() /*DBに格納されている日付にイベントを付与*/ for (i in 1..cursor.count) { val calendar = Calendar.getInstance() val strDate = cursor.getString(1) val dateFormat = SimpleDateFormat("yyyy-MM-dd") val date = dateFormat.parse(strDate) calendar.setTime(date) events.add(EventDay(calendar, R.drawable.icon) cursor.moveToNext() } calendarView.setEvents(events) /*例えばここでイベントが付与されている日数を取得したければ↓で済むと思います。*/ イベントが付与されている日数=DBに格納されてある数=cursor.count() /*しかし、月毎にイベントが付与されている日数を取得したい場合、どのような処理で 取得できるのかわからない状況です。calenderから月を指定してイベント数を取得する 方法等はあるのでしょうか? } } } }

あなたの回答
tips
プレビュー