add a function for getting event types from db

This commit is contained in:
tibbi 2017-02-11 12:31:12 +01:00
parent b77d71771c
commit 4d49c14f44
2 changed files with 33 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.activities
import android.os.Bundle
import com.simplemobiletools.calendar.R
import com.simplemobiletools.calendar.helpers.DBHelper
import com.simplemobiletools.commons.extensions.updateTextColors
import kotlinx.android.synthetic.main.activity_manage_event_types.*
@ -9,6 +10,11 @@ class ManageEventTypesActivity : SimpleActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_manage_event_types)
DBHelper.newInstance(applicationContext).getEventTypes {
}
updateTextColors(manage_event_types_scrollview)
}
}

View File

@ -338,6 +338,33 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return events
}
fun getEventTypes(callback: (types: List<EventType>) -> Unit) {
Thread({
fetchEventTypes(callback)
}).start()
}
fun fetchEventTypes(callback: (types: List<EventType>) -> Unit) {
val eventTypes = ArrayList<EventType>(3)
val cols = arrayOf(COL_TYPE_ID, COL_TYPE_TITLE, COL_TYPE_COLOR)
var cursor: Cursor? = null
try {
cursor = mDb.query(TYPES_TABLE_NAME, cols, null, null, null, null, COL_TYPE_ID)
if (cursor?.moveToFirst() == true) {
do {
val id = cursor.getIntValue(COL_TYPE_ID)
val title = cursor.getStringValue(COL_TYPE_TITLE)
val color = cursor.getIntValue(COL_TYPE_COLOR)
val eventType = EventType(id, title, color)
eventTypes.add(eventType)
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
callback.invoke(eventTypes)
}
interface EventUpdateListener {
fun eventInserted(event: Event)