DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Data Engineering
  3. Databases
  4. Using Kotlin in Android Studio 3.0 (Part 6 — Final)

Using Kotlin in Android Studio 3.0 (Part 6 — Final)

This series culminates with a guide to lambdas and database management when developing with Kotlin in Android Studio 3.X.

Ngoc Minh Tran user avatar by
Ngoc Minh Tran
CORE ·
Jun. 11, 18 · Tutorial
Like (3)
Save
Tweet
Share
10.11K Views

Join the DZone community and get the full member experience.

Join For Free

Lambdas

Lambdas are one of the most powerful tools in Kotlin since it allows defining an anonymous function and preventing us from having to write the specification of the function in an abstract class or interface, and then the implementation of the class. Let’s look at the following example using anonymous classes:

we first need to create an interface

interface MathOperation{
    fun operation( x:Int,  y:Int):Int
}

and then we write anonymous classes that implement this interface

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
   super.onCreate(savedInstanceState)
   setContentView(R.layout.activity_main)
   var x:Int = 3
   var y:Int = 3
   var resultOfAddition:Int = performOperation(x,y,
                   //anonymous class
                  object:MathOperation{
                       override fun operation(x: Int, y: Int): Int {
                                   return x+y;
                       }
                  });
   var resultOfSubtraction:Int = performOperation(x,y,
                 //anonymous class
                 object:MathOperation{
                      override fun operation(x: Int, y: Int): Int {
                                   return x-y;
                     }
                 });
   txt.text = "Addition is $resultOfAddition and Subtraction is $resultOfSubtraction"
}

fun performOperation(x:Int, y:Int, opt:MathOperation):Int{
     return opt.operation(x,y)
}
}

Now, we will consider the following example that uses lambda expressions

class MainActivity : AppCompatActivity() {

   override fun onCreate(savedInstanceState: Bundle?) {
     super.onCreate(savedInstanceState)
     setContentView(R.layout.activity_main)
     var x:Int = 3
     var y:Int = 3
     var performOperation = {x+y}
     val resultOfAddition = performOperation()
     performOperation = {x-y}
     val resultOfSubtraction = performOperation()
     txt.text = "Addition is $resultOfAddition and Subtraction is $resultOfSubtraction"
  }
}

Clearly, with lambdas we can simplify our code a lot.

Database Management

As you may know, Android uses SQLite as a database management system. SQLite is a relational database management system (RDBMS). If most RDBMSs such as MySQL, Oracle, etc. are standalone server processes, then SQLite is embedded because it is provided in the form of a library that is linked in applications.

If you have never created a database Android application before, you can refer to the my article (and get Java code). You can create a database Android application with Kotlin classes are used in this application like Java classes are used in my article but there are some changes (and source code, of course) as follows:

  • Student.java  -> Student.kt

  • MyDBHandler.java -> MyDBHandler.kt

  • MainActivity.java -> MainActivity.kt

And you can create a Kotlin class by selecting the package that will contain this class, right clicking and choosing New > Kotlin File/Class:

Image title

Fill in Name and change it from File to Class for Kind:

Image title

You can get the completed Kotlin source here.

Conclusion

This is the last article of my series (part 1, part 2, part 3, part 4, part 5, and this part). I hope that my series will help your familiarity with Kotlin and how to use it in the Android Studio 3.0 environment (or 3.X). 

Kotlin (programming language) Android Studio Android (robot) Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Kubernetes vs Docker: Differences Explained
  • Top 5 Node.js REST API Frameworks
  • A Guide To Successful DevOps in Web3
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: