Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Pair being used but method expects android.util.Pair

I have a PreferencesManager written in Java with the following method:

 public void insert(Pair<String, Object> keyValue, boolean async) {

And from kotlin I'm trying to insert a new Pair into the PreferencesManager like so:

 val  p = Pair(SharedPreferencesManager.STATIC_KEY, value)
            preferencesManager!!.insert(p, false)

However i'm getting an error saying:

"Required android.util.Pair, found kotlin.Pair"...

Is there a way I can convert from one to the other?

like image 465
davy307 Avatar asked Oct 01 '18 14:10

davy307


2 Answers

As an alternative to the answer @TheWanderer gave in the comments of the question. I would suggest importing android.util.Pair using the as keyword as follows:

import android.util.Pair as APair
//....
val  p = APair(SharedPreferencesManager.STATIC_KEY, value)
preferencesManager!!.insert(p, false)

See the kotlin docs on importing for more. The kotlin package is always going to imported, and apparently will take precedence over classes from other packages unless you rename them or fully qualify them.

like image 137
whaley Avatar answered Oct 10 '22 22:10

whaley


Kotlin has its own Pair, which isn't an extension or proxy for android.util.Pair. Try using a helper method instead:

public void insert(String key Object value, boolean async) {
    Pair<String, Object> pair = new Pair<>(key, value);
    insert(pair, async);
}
like image 35
TheWanderer Avatar answered Oct 10 '22 22:10

TheWanderer