add a helper function for filling SelectEventTypeDialog

This commit is contained in:
tibbi 2017-02-11 21:22:25 +01:00
parent 8783b674a7
commit 31cff330a6

View File

@ -16,23 +16,18 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
val dialog: AlertDialog?
var wasInit = false
var eventTypes = ArrayList<EventType>()
var radioGroup: RadioGroup
init {
val view = activity.layoutInflater.inflate(R.layout.dialog_radio_group, null)
val radioGroup = view.dialog_radio_group
radioGroup = view.dialog_radio_group
radioGroup.setOnCheckedChangeListener(this)
DBHelper.newInstance(activity).getEventTypes {
activity.runOnUiThread {
eventTypes = it
activity.runOnUiThread {
eventTypes.forEach {
val radioButton = activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton
radioButton.apply {
text = it.title
isChecked = it.id == currEventType
id = it.id
}
radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
addRadioButton(it)
}
wasInit = true
}
@ -44,6 +39,15 @@ class SelectEventTypeDialog(val activity: Activity, val currEventType: Int, val
}
}
private fun addRadioButton(type: EventType) {
val radioButton = (activity.layoutInflater.inflate(R.layout.radio_button, null) as RadioButton).apply {
text = type.title
isChecked = type.id == currEventType
id = type.id
}
radioGroup.addView(radioButton, RadioGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT))
}
override fun onCheckedChanged(group: RadioGroup, checkedId: Int) {
if (wasInit) {
callback.invoke(checkedId)