如何从xml传递字符串作为第二个参数?

时间:2018-01-15 16:36:03

标签: android-databinding

Android Studio 3.1,Java 1.8,Gradle 4.1

这里我的xml带有数据绑定( point_of_sale_list_item )。

<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="android.view.View" />    
        <import type="com.myproject.util.date.DateUtil" />
        <variable   name="item"  type="com.myproject.api.model.PointOfSale"/>

    </data>


<TextView
            android:id="@+id/dateTextView"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:text="@{DateUtil.getDateAsString(item.date,MM/yy)}"/>

</layout>

这是我的自定义方法:

public static String getDateAsString(Date date, String pattern) {
    if (date == null) {
        return "null";
    }
    SimpleDateFormat sdf = null;
    try {
        sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    } catch (IllegalArgumentException ex) {
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

}

当我尝试从xml调用我的方法时,我收到错误:

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> android.databinding.tool.util.LoggedErrorException: Found data binding errors.
  ****/ data binding error ****msg:Identifiers must have user defined types from the XML file. MM is missing it
  file:D:\dev\My Project\app\src\main\res\layout\point_of_sale_list_item.xml
  loc:53:63 - 53:64
  ****\ data binding error ****

0 个答案:

没有答案