Select CRUD operation

The select operation will read and return data from the database. What follows is its implementation:

     val NOTE = object : Crud<Note> { 
        ... 
        override fun select( 
            args: Pair<String, String> 
        ): List<Note> = select(listOf(args)) 
 
        override fun select(args: Collection<Pair<String, String>>): List<Note> { val db = DbHelper(name, version).writableDatabase val selection = StringBuilder() val selectionArgs = mutableListOf<String>() args.forEach { arg -> selection.append("${arg.first} == ?") selectionArgs.add(arg.second) } val result = mutableListOf<Note>() val cursor = db.query( true, DbHelper.TABLE_NOTES, null, selection.toString(), selectionArgs.toTypedArray(), null, null, null, null ) while (cursor.moveToNext()) { val id = cursor.getLong(cursor.getColumnIndexOrThrow ...

Get Mastering Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.