現在、日付を文字入力する仕様になっているのですが、日付選択ダイアログにする方法がわかりません。
以下ソース
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var realm: Realm
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
realm = Realm.getDefaultInstance()
val schedules = realm.where<Schedule>().findAll()
listView.adapter = ScheduleAdapter(schedules)
fab.setOnClickListener { view -> startActivity<ScheduleEditActivity>() Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } listView.setOnItemClickListener { parent, view, position, id -> val schedule = parent.getItemAtPosition(position) as Schedule startActivity<ScheduleEditActivity>("schedule_id" to schedule.id) } } override fun onDestroy() { super.onDestroy() realm.close() }
}
ScheduleEditActivity
class ScheduleEditActivity : AppCompatActivity() {
private lateinit var realm: Realm
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_schedule_edit) realm = Realm.getDefaultInstance() val scheduleId = intent?.getLongExtra("schedule_id", -1L) if(scheduleId != -1L) { val schedule = realm.where<Schedule>() .equalTo("id", scheduleId).findFirst() dateEdit.setText( DateFormat.format("yyyy/MM/dd", schedule?.date) ) titleEdit.setText(schedule?.title) detailEdit.setText(schedule?.detail) delete.visibility = View.VISIBLE } else { delete.visibility = View.INVISIBLE } save.setOnClickListener { when(scheduleId) { -1L -> { realm.executeTransaction { val maxId = realm.where<Schedule>().max("id") val nextId = (maxId?.toLong() ?: 0L) + 1 val schedule = realm.createObject<Schedule>(nextId) dateEdit.text.toString().toDate("yyyy/MM/dd")?.let { schedule.date = it } schedule.title = titleEdit.text.toString() schedule.detail = detailEdit.text.toString() } alert("追加しました") { yesButton { finish() } }.show() } else -> { realm.executeTransaction { val schedule = realm.where<Schedule>() .equalTo("id", scheduleId).findFirst() dateEdit.text.toString().toDate("yyyy/MM/dd")?.let { schedule?.date = it } schedule?.title = titleEdit.text.toString() schedule?.detail = detailEdit.text.toString() } alert("修正しました") { yesButton { finish() } }.show() } } } delete.setOnClickListener { realm.executeTransaction { realm.where<Schedule>().equalTo("id", scheduleId) ?.findFirst() ?.deleteFromRealm() } alert("削除しました") { yesButton { finish() } }.show() } } override fun onDestroy() { super.onDestroy() realm.close() } fun String.toDate(pattern: String = "yyyy/MM/dd HH:mm"): Date? { val sdFormat = try { SimpleDateFormat(pattern) } catch (e: IllegalArgumentException) { null } val date = sdFormat?.let { try { it.parse(this) } catch (e: ParseException) { null } } return date }
}
Dialog
class DatePickerFragment : DialogFragment(),
DatePickerDialog.OnDateSetListener {
interface OnDateSelectedListener { fun onSelected(year: Int, month: Int, date: Int) } private lateinit var listener: OnDateSelectedListener override fun onAttach(context: Context?) { super.onAttach(context) if(context is OnDateSelectedListener) { listener = context } } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val c = Calendar.getInstance() val year = c.get(Calendar.YEAR) val month = c.get(Calendar.MONTH) val date = c.get(Calendar.DAY_OF_MONTH) return DatePickerDialog(context, this, year, month, date) } override fun onDateSet(view: DatePicker, year: Int, month: Int, date: Int) { ```listener.onSelected(year, month, date)
} } 試したこと -- Dialogファイルを作成し、DatePickerFragment.OnDateSelectedListenerをMainActivityに書いたところ日付選択ダイアログは表示されたのですが、日付を選択した後落ちてしまいます。エラー内容(kotlin.UninitializedPropertyAccessException: lateinit property listener has not been initialized at com.example.myalarmclock.DatePickerFragment.onDateSet)