Search code examples
android-databindingandroid-livedata

Data binding generic variable and livedata


How to define Livedata genneric with type List ?

<import type="androidx.lifecycle.LiveData"/>

<import type="java.util.List"/>

<variable
        name="livedata"
        type="LiveData&lt;List>"/>

when compiling it throw error: incompatible types: Object cannot be converted to List but if i am using primitive type it work , how can i do this with List type?


Solution

  • You must infer the type of list, for example, if list is of type List of Integers you must do:

    <import type="androidx.lifecycle.LiveData"/>
    
    <import type="java.util.List"/>
    <import type="Integer"/>
    
    <variable
            name="livedata"
            type="LiveData&lt;List&lt;Integer&gt;&gt;"/>