https://kotlinlang.org logo
Title
r

rattleshirt

07/06/2018, 8:25 AM
Hi, how can I use an abstract class in databinding (3.2.0-beta02), it gives me a
Could not find accessor companyName
when using
android:text="@{data.invoice.companyName}"
(kotlin v1.2.51) with
abstract var companyName: String?
. In the kotlin bytecode I see a
getCompanyName()
method but that doesn’t work either
a

arekolek

07/06/2018, 8:31 AM
Maybe you have
data.getInvoice()
that is chosen over
data.invoice
and doesn't have the
companyName
. That was the problem here https://stackoverflow.com/questions/32683129/android-databinding-error-could-not-find-accessor
r

rattleshirt

07/06/2018, 9:14 AM
Doesn’t seem to be my case
g

gildor

07/06/2018, 9:15 AM
You can use abstract method, sure. Hard to see what is your problem, maybe you could to reproduce on some sampl
r

rattleshirt

07/06/2018, 9:16 AM
but also field?
g

gildor

07/06/2018, 9:16 AM
Databinding lib does the same what Kotlin, you can use `companyName`instead of
getCompanyName()
maybe your problem with invoice method
maybe invoice type doesn’t contain companyName property
r

rattleshirt

07/06/2018, 9:18 AM
This is what I have
abstract class InvoiceType {
    abstract var companyName: String?
}
interface AfterpayInvoice<T : InvoiceType> {
    var invoice: T
}
with
data class Invoice(@PrimaryKey
    var orderNumber: String,
    override var companyName: String?
) : InvoiceType()
class InvoiceWithDetails : AfterpayInvoice<Invoice> {
    @Embedded
    override lateinit var invoice: Invoice
}
It can find the invoice
It probably doesn’t know the type of invoice yet because of the generic
g

gildor

07/06/2018, 9:36 AM
Yes, generic can be a problem
r

rattleshirt

07/06/2018, 9:37 AM
It is 😞
interface AfterpayInvoice<T : InvoiceType> {
    var invoice: T

    fun getterForInvoice() : InvoiceType
}
this seems to work
g

gildor

07/06/2018, 9:59 AM
You can fix it by using non-generic version of your
data
so instead type of your ViewModel SomeData<InvoiceType> (or how does it work) You can use
class InvoiceSomeData : SomeData<InvoiceType>
r

rattleshirt

07/06/2018, 10:15 AM
Sorry, I don’t follow
g

gildor

07/06/2018, 10:49 AM
It can be a problem of type check with generic on compile time and because of type erasure you cannot use model with generic, I saw something similar, so to fix this you can use ViewModel without generic param