user11747064
user11747064

Reputation:

How to pass value between kotlin class and java class in android?

I have a class one in java which implements AsyncTask and other is kotlin MainActivity.

Now I have a kotlin function with kotlin int as parameter in MainActivity. I have managed to call kotlin function in java class but unable to pass Java Integer,it shows compile time error. Is there any way to pass integer value?

Upvotes: 2

Views: 1172

Answers (3)

Bach Vu
Bach Vu

Reputation: 2348

From your post, I guess that the compile error occurred due to using int instead of Int in Kotlin. Change your kotlin function to this

fun kotlinfun(i: Int){
    ...
}

and call normally in java class

int k=0;
obj.kotlinfun(k);

Upvotes: 1

Xavi Jimenez
Xavi Jimenez

Reputation: 332

Try it:

int k=0;
obj.kotlinfun(String.valueOf(k));
  • Convert using Integer.toString(int)
  • Convert using String.valueOf(int)
  • Convert using new Integer(int).toString()
  • Convert using String.format()
  • Convert using StringBuffer or StringBuilder.

Upvotes: 0

Septian Triadi
Septian Triadi

Reputation: 171

Java using int instead Integer,
and in java to parse integer to string using Integer.toString(k)

Upvotes: 2

Related Questions