I want to bind one of my utils class to a textview. I included my Utils class in data field. When I run the app I am getting error like the method is not found
My xml file
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="PACKAGE_NAME.utils.CommonUtils"/>
<variable
name="viewModel"
type="PACKAGE_NAME.PACKAGE_UI_PATH.RecordItemViewModel" />
</data>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:elevation="5dp"
app:cardElevation="5dp"
tools:targetApi="lollipop">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14.5sp"
android:textStyle="normal"
android:textColor="#66000000"
android:id="@+id/dateTv"
android:lineSpacingExtra="4.5sp"
android:gravity="center_horizontal"
android:text="@{CommonUtils.returnDiffFromCurrentDate(viewModel.mDate)}
" tools:text="1 day ago"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</layout>
And my utility file is
public final class CommonUtils {
private CommonUtils() {
// This utility class is not publicly instantiable
}
public static String returnDiffFromCurrentDate(long givenTime)
{
long timeGap = System.currentTimeMillis() - givenTime;
return String.valueOf(TimeUnit.MILLISECONDS.toDays(timeGap));
}
}
I got the following erroe message
Found data binding errors. ****/ data binding error ****msg:cannot find method returnDiffFromCurrentDate(java.lang.String) in class PACKAGE_NAME.utils.CommonUtils
The error says it can not find a method called returnDiffFromCurrentDate with a single String parameter, not long as it is in your CommonUtils class.
The problem then is in viewModel.mDate what seems to be a String, and not a long
You should use any other method in order to transform it to a long or change returnDiffFromCurrentDate to expect a String