Search code examples
javaandroidrealmandroid-databindingrealm-list

How to get an item from a RealmList using android databinding


I have a Car class implemented like this :

        public class Car extends RealmObject implements Serializable {
       @SerializedName("car_details")
        @Expose
        private RealmList<CarDetail> carDetails = null;
  public RealmList<CarDetail> getCarDetails() {
        return carDetails;
    }

    public void setCarDetails(RealmList<CarDetail> carDetails) {
        this.carDetails = carDetails;
    }
    }

in My trying to access my carDetails from my Layout like this :

<TextView
                                                      android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="8dp"
                            android:text="@{car.carDetails.get(0).grade}"
                            android:textAllCaps="true"
                            android:textColor="@color/black"
                            android:textSize="30sp"
                            android:textStyle="bold" />

but i'm getting an error when compiling , any suggestions how to access the list?

i also tried with :

android:text="@{car.carDetails[0].grade}"

I'm getting an error like this :

error: package me.test.databinding does not exist

It's cannot generate the databinding classes

Capture from the error: enter image description here


Solution

  • Seeing you error log screenshot shows the actual problem

    Your error: com.sun.tools.javac.code.Symbol$CompletionFailure: class file for rx.Observable not found

    FIX:

    This can be fixed by either adding RxJava to your project or create an empty dummy file that looks like the following.

    package rx;
    
    public class Observable {
        // Dummy class required for Jackson-Databind support if
       // RxJava is not a project dependency.
    }
    

    Reference:

    See this, this and this