https://kotlinlang.org logo
#android
Title
# android
d

david.bilik

05/09/2017, 10:51 AM
Hi, do you know if I can use delegated properties for things like properties in adapter? I am always writing code like this
Copy code
var products: List<Product> = listOf()
        set(value) {
            field = value
            notifyDatasetChanged()
        }
and I think that I can use delegated properties with some syntax like
var products: List<ApiProduct> by adapterProperty(initialValue = listOf())
but I dont have such experience with them
u

umar

05/09/2017, 6:21 PM
david.bilik:
Copy code
import kotlin.reflect.KProperty
class AdapterProperty<T>(private var value: T) {
    operator fun getValue(thisRef: Any?, property: KProperty<*>) = value
    operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: T) {
        value = newValue
        notifyDatasetChanged()
}
Do you understand my example?
d

david.bilik

05/10/2017, 5:49 AM
Hi, thanks 🙂 yes I understand, i’ve looked ad Delegates.observable delegate and done it the same way