Kotlin SQLite example – CRUD operations with ListView | Android

SQLite is an Open Source database that supports standard relational database features like SQL syntax, transactions and prepared statements. Each column in an SQLite database is assigned one of data types: NULL, INTEGER, REAL, TEXT, BLOB. In this tutorial, we’re gonna look at how to make SQLite CRUD operations when interacting with ListView.

Related Posts:
Kotlin ListView example | Android
Kotlin GridView example: Show List of Items on Grid | Android

More Practice: Kotlin SQLite example – Search with Listview | Android

I. Technologies

– Android Studio 3
– Kotlin 1.1.51

II. Overview

1. Goal

We will build an Android App that supports showing, inserting, editing, deleting Notes from/to SQLite Database with ListView:

kotlin-sqlite-goal

2. SQLite
2.1 SQLiteDatabase

SQLiteDatabase is the base class for working with a SQLite Database in Android. It provides:
insert(), update(), delete() method
execSQL() to execute an SQL statement directly

To work with SQLite Database, we create a subclass of SQLiteOpenHelper class:
– constructor: call super() method to specify database_name, version and context
– override onCreate() to execute CREATE TABLE command
– override onUpgrade() to handle database version increment
– indicate read/write mode by using getReadableDatabase() or getWritableDatabase() method

For example:

2.2 Query

A query can be created via:
rawQuery() or query()
SQLiteQueryBuilder

For example:

2.3 ContentValues

ContentValues object can be used for insert and update database entries.

For example:

3. Project Structure

kotlin-sqlite-structure

III. Practice

1. Set up Project

– Create New Project with package name com.javasampleapproach.kotlin.sqlite and Kotlin support.
– Add images (found in source code) to drawable.

2. Layout
2.1 Main Activity

Open res/layout/activity_main.xml file:

2.2 Item Layout

Add note.xml layout file to res/layout:

2.3 Activity Layout for Adding/Updating Note

Add activity_note.xml layout file to res/layout:

2.4 Menu

Under res folder, create menu folder and add menu_main.xml:

3. Data Model

Add Kotlin class:

4. Database Manager

5. Activity
5.1 Main Activity

To work with ListView, we will:
– create ViewHolder class
– create BaseAdapter subclass
– set adapter for ListView
– add Listener to ListView

To work with Add Note button on Menu, we will:
– override onCreateOptionsMenu() function to specify menu layout resource
– override onOptionsItemSelected function to launch NoteActivity

To display List of Items, we will create loadQueryAll() and call it inside onCreate() and onResume().

To support edit Note and delete Note, we register a callback for ivEdit and ivDelete ImageView.
With edit Note, to transfer data between 2 Activity, we use Intent.

5.2 Note Activity

IV. Source Code

Kotlin-SQLite

By JavaSampleApproach | December 9, 2017.

Last updated on July 13, 2018.


Related Posts


9 thoughts on “Kotlin SQLite example – CRUD operations with ListView | Android”

  1. It only builds, wenn I extend findViewById() to findViewById<TextView/ImageView>() in the ViewHolder Class.
    Otherwhise I get a “Error:(154, 34) Type inference failed: Not enough information to infer parameter T in fun <T : View!> findViewById(p0: Int): T!
    Please specify it explicitly.”

    The App stopps when I tap on the addNote in the menu and dunno where to debug. Sorry! Absolute Beginner.

    1. Hi Rafael,

      With API 26, we should change:

      to:

      Regards,
      JSA.

  2. I was going through the previous tutorials “by hand”. Due to the issues I had here and I’m not sure if I made a mistake I tried to import/load the downloadable sourcecode.
    Unfortunately, that did not work as intended. Is there a simple way to import the source als project I can work on?

    Thanks in advance.

    1. Hi Rafael,

      Please use:

      Regards,
      JSA.

  3. It took my several hours to find, why my build App always crashes.
    Finally – after finding LogCat and using it – it gives me something like “Unable to find explicit activity class {}; have you declared this activity in your AndroidManifest.xml”
    The NoteActivity was not declared. All I had to do was a Alt+Enter on the class to declare it.
    The App is running – I’m improving 🙂

  4. How can I run the code? I’m new in Android, I downloaded the code but there is required create a configuration to run the code….Some body can help me. Thanks!

  5. I went through you tutorial and got it working, but I decidied to change all your references to Notes to League, to see if I understood it completely. However I am getting an Unresolved Reference to lvLeague in the MainActivity.kt.

    I have gone back through your tutorial and I am unable to find any where else lvNotes is being used.

    Any assistance would be helpful. Thanks!

Got Something To Say:

Your email address will not be published. Required fields are marked *

*