Less boilerplate Cursor data parsing
In this recipe, we are going to learn how to work with the Android Cursor
type in a more effective and easy way. We are going to create an extension function for the Cursor
type, allowing us to query it in a clean way. We will also implement a practical example showing how to access the system-content resolver in order to fetch contacts stored on the device and transform Cursor
into a list of strings representing the contacts' names.
Getting ready
You can examine the implementation and configuration of the recipes related to the Android framework in the AndroidSamples project available in the GitHub repository: https://github.com/PacktPublishing/Kotlin-Standard-Library-Cookbook/. To follow Android-related recipes, you just need to create a new project in Android Studio.
How to do it...
- Implement an extension function that allows us to fetch the values of a requested column name from
Cursor
:
fun Cursor.getString(columnName: String): String? { return getString...