Hi, how can I use an abstract class in databinding...
# android
r
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
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
Doesn’t seem to be my case
g
You can use abstract method, sure. Hard to see what is your problem, maybe you could to reproduce on some sampl
r
but also field?
g
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
This is what I have
Copy code
abstract class InvoiceType {
    abstract var companyName: String?
}
Copy code
interface AfterpayInvoice<T : InvoiceType> {
    var invoice: T
}
with
Copy code
data class Invoice(@PrimaryKey
    var orderNumber: String,
    override var companyName: String?
) : InvoiceType()
Copy code
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
Yes, generic can be a problem
r
It is 😞
Copy code
interface AfterpayInvoice<T : InvoiceType> {
    var invoice: T

    fun getterForInvoice() : InvoiceType
}
this seems to work
g
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
Sorry, I don’t follow
g
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