jw
01/21/2016, 4:00 AMdebug
01/21/2016, 4:08 AMdebug
01/21/2016, 4:27 AMdebug
01/21/2016, 4:29 AMjuancho
01/21/2016, 10:01 PMeric
01/21/2016, 10:06 PMkotlin.List
, and you need a java.util.ArrayList
.juancho
01/21/2016, 10:08 PMeric
01/21/2016, 10:12 PMdamian
01/21/2016, 10:22 PMArrayList
(or you don't know and don't mind making a copy), you can always use .toArrayList()
juancho
01/21/2016, 11:58 PMkirillrakhman
01/22/2016, 10:48 AMTextView
as delegate for a CharSequence
property:
operator fun TextView.getValue(thisRef: Any, property: KProperty<*>) = text
operator fun TextView.setValue(thisRef: Any, property: KProperty<*>, value: CharSequence) {
text = value
}
Using the Android Extensions, I thought the following should work:
import kotlinx.android.synthetic.main....
var foo: CharSequence by editTextFoo
However, as soon as I call the property, I get a java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter $receiver
at package.name.DelegateUtilsKt.getValue(DelegateUtils.kt:65532)
the culprit seems to be that when the property is initialized, the view isn't actually inflated so the property is delegated to null
. Any ideas how I could get around this without declaring an additional delegate class?kirillrakhman
01/22/2016, 10:49 AMyoavst
01/22/2016, 10:53 AMlazy
kirillrakhman
01/22/2016, 10:56 AMLazy<TextView>
?kirillrakhman
01/22/2016, 10:59 AMyoavst
01/22/2016, 11:00 AMkirillrakhman
01/22/2016, 11:00 AMvar foo: CharSequence
get() = editTextFoo.text
set(value) {
editTextFoo.text = value
}
works but is not as terseyoavst
01/22/2016, 11:00 AMkirillrakhman
01/22/2016, 11:00 AMkirillrakhman
01/22/2016, 11:01 AMyoavst
01/22/2016, 11:02 AMyoavst
01/22/2016, 11:03 AMval TextView.delegation: Lazy<CharSequence>
get() = lazy { text }
var foo by editTextFoo.delegation
kirillrakhman
01/22/2016, 11:03 AMkirillrakhman
01/22/2016, 11:04 AMyoavst
01/22/2016, 11:04 AMyoavst
01/22/2016, 11:05 AMkirillrakhman
01/22/2016, 11:05 AMkirillrakhman
01/22/2016, 11:05 AMyoavst
01/22/2016, 11:06 AMReadWriteProperty
object that is equivalent to your long code.kirillrakhman
01/22/2016, 11:07 AMclass TextDelegate(private val id: Int) : ReadWriteProperty<View,CharSequence> {
override fun getValue(thisRef: View, property: KProperty<*>) = (thisRef.findViewById(id) as TextView).text
override fun setValue(thisRef: View, property: KProperty<*>, value: CharSequence) {
(thisRef.findViewById(id) as TextView).text = value
}
}
val foo by TextDelegate(R.id.editTextFoo)