Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin set value of global variable

We are trying to set the value of a global variable declared in the code below

 class MyApplication: Application() {
    var globalVar = 2
}

Now we have a Main Activity that has a Edit Text named etPerson we would like to enter a value in etPerson and set the entered value equal to globalVar
Why because we want to make a call to a database function in another Activity
Here is the code that makes the call to the DB

    var mApp = MyApplication()
    var intGlobalVar = mApp.globalVar
    println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@ globalINT = "+intGlobalVar)
    var ITEM = db.getItem(intGlobalVar)
    println("%%%%%%%%%%%%%%%%%% ITEM "+ITEM?.id+" name "+ITEM?.name)

And for clarity here is the DB function

    fun getItem(id: Int): Contact? {
    val db = this.writableDatabase
    val selectQuery = "SELECT  * FROM $TABLE_NAME WHERE $colId = ?"
    db.rawQuery(selectQuery, arrayOf(id.toString())).use {
        // .use requires API 16
        if (it.moveToFirst()) {
            val result = Contact(0)
            result.id = it.getInt(it.getColumnIndex(colId))
            result.name = it.getString(it.getColumnIndex(colName))
            return result
        }
    }
    return null
}

So the issue is setting the var globalVar to the value entered in etPerson on the Main Activity
The concept can be accomplished using put and get with intents but that is not our goal here

Our question is how to set the globalVar to the value entered in the Edit Text?

like image 965
Vector Avatar asked Oct 16 '18 21:10

Vector


People also ask

How do you set a global variable in Kotlin?

This example demonstrates how to declare global variables on Android using Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you assign a value to a variable in Kotlin?

Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val . Use var for a variable whose value can change.

Are there global variables Kotlin?

In Kotlin, A global variable is a variable that is declared at the top of the program and outside all functions similar to C and C++. A local variable can only be used in the particular block where it is declared. A global variable can be used in all functions.

What is Lateinit in Kotlin?

The lateinit keyword allows you to avoid initializing a property when an object is constructed. If your property is referenced before being initialized, Kotlin throws an UninitializedPropertyAccessException , so be sure to initialize your property as soon as possible.


1 Answers

When your app starts one and only one object of the class MyApplication() will be created, so you don't need:

var mApp = MyApplication()

You can access MyApplication() and all its members from everywhere in your app.

Declare a companion object in MyApplication() and put globalVar's declaration inside:

class MyApplication: Application() {    
    companion object {
        var globalVar = 2
    } 

    override fun onCreate() {
        super.onCreate()
        // initialization code here
    }
}

So in your MainActivity class or elsewhere you can use MyApplication.Companion.globalVar to get or set its value.
Or you can import the globalVar variable in any class like:

import yourpackagename.MyApplication.Companion.globalVar

and refer to it simply globalVar

You also need to declare MyApplication in the manifest:

  <application
        android:name=".MyApplication"
like image 63
forpas Avatar answered Sep 17 '22 21:09

forpas