Home Can't create current date in xml: mismatched input 'Date'
Reply: 0

Can't create current date in xml: mismatched input 'Date'

user3540
1#
user3540 Published in June 18, 2018, 8:45 pm

Android Studio 3.0. I use data binding.

An here my xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

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

        <import type="com.myproject.util.date.DateUtil" />

        <variable
            name="item"
            type="com.myproject.model.Purchase" />

    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/validStillValueTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:layout_marginStart="5dp"
                            android:layout_marginTop="10dp"
                            android:textColor='@{DateUtil.getDiffDays(item.product.dateUntil, item.product.dateUntil) == 0 ? @color/strikethrough : @color/textColorPrimary}'
                            android:textSize="9sp"
                            android:textStyle="bold"
                            app:layout_constraintStart_toEndOf="@+id/validStillLabelTextView"
                            app:layout_constraintTop_toBottomOf="@+id/separator2" />

    </android.support.constraint.ConstraintLayout>
</layout>

Here method:

public static long getDiffDays(Date dateFrom, Date dateTo) {
        long diff = getDiffMilliseconds(dateFrom, dateTo);
        return diff / (24 * 60 * 60 * 1000);
}

And as result it's work fine. Nice.

But now I want to pass current date (by new Date()) as second parameters in xml file.

So here changes in xml:

<TextView
                            android:id="@+id/validStillValueTextView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:layout_marginStart="5dp"
                            android:layout_marginTop="10dp"
                            android:textColor='@{DateUtil.getDiffDays(item.product.dateUntil, new Date()) == 0 ? @color/strikethrough : @color/textColorPrimary}'
                            android:textSize="9sp"
                            android:textStyle="bold"
                            app:layout_constraintStart_toEndOf="@+id/validStillLabelTextView"
app:layout_constraintTop_toBottomOf="@+id/separator2" />

But now I get error:

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> android.databinding.tool.util.LoggedErrorException: Found data binding errors.
  ****/ data binding error ****msg:Syntax error: mismatched input 'Date' expecting {',', ')', '.', '::', '[', '+', '-', '*', '/', '%', '<<', '>>>', '>>', '<=', '>=', '>', '<', 'instanceof', '==', '!=', '&', '^', '|', '&&', '||', '?', '??'}
  file:myproject\app\src\main\res\layout\purchase_details.xml
  loc:207:49 - 207:158
  ****\ data binding error ****
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31782 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO