Skip to main content

Posts

android kotlin - Room LIKE query example

MainActivity.kt package com.example.roomexamples import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.method.ScrollingMovementMethod import com.android.volley.Request import com.android.volley.Response import com.android.volley.toolbox.JsonArrayRequest import com.android.volley.toolbox.Volley import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast import org.jetbrains.anko.uiThread class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val room = RoomSingleton.getInstance(this) textView.movementMethod = ScrollingMovementMethod() // Insert some products in database btnInsert.setOnClickListener { it.isEnabled = false val url = "https://pastebin.com/raw/BrQjM5dX" v...

android kotlin - Room boolean column example

MainActivity.kt package com.example.roomexamples import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.TextView import com.android.volley.Request import com.android.volley.Response import com.android.volley.toolbox.JsonArrayRequest import com.android.volley.toolbox.Volley import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast import org.jetbrains.anko.uiThread import java.text.DateFormat import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val room = RoomSingleton.getInstance(this) textView.movementMethod = ScrollingMovementMethod() // Insert some products in database btnInsert.setOnClickListener { val url = "https://...

android kotlin - Room TypeConverter date example

MainActivity.kt package com.example.roomexamples import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.method.ScrollingMovementMethod import com.android.volley.Request import com.android.volley.Response import com.android.volley.toolbox.JsonArrayRequest import com.android.volley.toolbox.Volley import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast import org.jetbrains.anko.uiThread import java.text.DateFormat import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val room = RoomSingleton.getInstance(this) textView.movementMethod = ScrollingMovementMethod() // Insert some products in database btnInsert.setOnClickListener { val url = "https://tradeogre.com/api/v1/history/BT...

android kotlin - Room TypeConverter example

MainActivity.kt package com.example.roomexamples import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.method.ScrollingMovementMethod import com.android.volley.Request import com.android.volley.Response import com.android.volley.toolbox.JsonArrayRequest import com.android.volley.toolbox.Volley import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.toast import org.jetbrains.anko.uiThread class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val room = RoomSingleton.getInstance(this) textView.movementMethod = ScrollingMovementMethod() // Insert some products in database btnInsert.setOnClickListener { val url = "https://tradeogre.com/api/v1/markets" val request = JsonArrayRequest(...

android kotlin - Room select where example

MainActivity.kt package com.example.roomexamples import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.text.method.ScrollingMovementMethod import kotlinx.android.synthetic.main.activity_main.* import org.jetbrains.anko.doAsync import org.jetbrains.anko.uiThread class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val room = RoomSingleton.getInstance(this) textView.movementMethod = ScrollingMovementMethod() // Insert some books in database btnInsert.setOnClickListener { doAsync { room.bookDao().insertBooks(bookList()) } } // Select books from database btnSelect.setOnClickListener { doAsync { // Get books which price greater than $50 val books = room.bookDao().allBooks(5...

Android Kotlin: Room RecyclerView LiveData example

Introduction In modern Android app development, building efficient and responsive user interfaces that can handle large datasets is a crucial aspect. Combining components like RecyclerView for displaying data and Room for database management is a common practice, especially when you want to ensure smooth data flow and user experience. This example demonstrates how to use Room , RecyclerView , and LiveData in Kotlin to create an application where user inputs can be dynamically stored in a local database and displayed in real-time. This tutorial will walk through each part of the code in detail, from setting up the data model and database to integrating a view model and RecyclerView for efficient data handling. By the end of the description, you'll have a clear understanding of how these components work together to create a seamless Android application. MainActivity.kt In the MainActivity.kt , the app's primary logic is implemented. First, a StudentViewModel is retrieved using ...

Android Kotlin: Room LiveData ViewModel example

Introduction In Android development, integrating database operations with the user interface can be challenging, especially when aiming for clean and efficient code. Kotlin's support for Android Jetpack components, such as Room, ViewModel, and LiveData, provides an excellent solution for handling database-related tasks in a lifecycle-aware manner. This article demonstrates a practical example of how to use these components together in an Android application. Specifically, it shows how to implement a simple app where users can insert data into a Room database, and observe changes using LiveData, all managed by a ViewModel. The example covers the essential aspects of using Room for local storage, LiveData for observing data changes, and ViewModel for preserving UI-related data during configuration changes. The code includes database creation, a DAO interface for queries, and a ViewModel to manage UI data. MainActivity.kt Overview The MainActivity serves as the entry point of the app...