O'Reilly logo

Mastering Android Development with Kotlin by Miloš Vasić

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required