Android数据绑定双向转换方法

Dan*_*nce 2 data-binding android android-databinding

我有这个名为Item的对象

public class Item extends BaseObservable
{

    private double dose;
    private Integer reportedDose;

    @Bindable
    public double getDose() {
        return dose;
    }

    public void setDose(double dose) {
        this.dose = dose;
        notifyPropertyChanged(BR.dose);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在关注@GeorgeMount上一篇教程:https://medium.com/google-developers/android-data-binding-inverse-functions-95aab4b11873 我添加了Converter类:

public class Converter
{
    @InverseMethod("toDouble")
    public static String toString(TextView view, double oldValue,
                                  double value) {
        NumberFormat numberFormat = getNumberFormat(view);
        try {
            // Don't return a different value if the parsed value
            // doesn't change
            String inView = view.getText().toString();
            double parsed =
                    numberFormat.parse(inView).doubleValue();
            if (parsed == value) {
                return view.getText().toString();
            }
        } catch (ParseException e) {
            // Old number was broken
        }
        return numberFormat.format(value);
    }

    public static double toDouble(TextView view, double oldValue,
                                  String value) {
        NumberFormat numberFormat = getNumberFormat(view);
        try {
            return numberFormat.parse(value).doubleValue();
        } catch (ParseException e) {
            Resources resources = view.getResources();
            //String errStr = resources.getString(R.string.badNumber);
            view.setError("error");
            return oldValue;
        }
    }

    private static NumberFormat getNumberFormat(View view) {
        Resources resources= view.getResources();
        Locale locale = resources.getConfiguration().locale;
        NumberFormat format =
                NumberFormat.getNumberInstance(locale);
        if (format instanceof DecimalFormat) {
            DecimalFormat decimalFormat = (DecimalFormat) format;
            decimalFormat.setGroupingUsed(false);
        }
        return format;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想在我的activity_main.xml中的EditText中使用它:

<?xml version="1.0" encoding="utf-8"?>
<layout>

    <data>

        <variable
            name="item"
            type="com.example.dan.bindtest.Item" />

        <variable
            name="Converter"
            type="com.example.dan.bindtest.Converter"/>
    </data>

    <android.support.constraint.ConstraintLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.dan.bindtest.MainActivity"
        android:padding="30dp">

        <EditText
            android:id="@+id/first"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:text="@={Converter.toString(first, item.dose, item.dose)}"
            android:hint="Enter a dose..."
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/second"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:hint="Enter a dose..."
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

但问题是,即使我按照一步一步的说明执行此错误:

"Error:cannot generate view binders java.lang.ClassCastException: android.databinding.tool.expr.IdentifierExpr cannot be cast to android.databinding.tool.expr.StaticIdentifierExpr"
Run Code Online (Sandbox Code Playgroud)

如果我从EditText中删除android:text = ...行,它编译时没有错误.此外,我试图删除@Bindable注释和notifyPropertyChanged(BR.dose)行仍然有同样的问题.我有这个代码在公共存储库中,如果有人想检查它:https://github.com/danponce/bindtest任何人都有线索?@GeorgeMount?

Urv*_*ana 8

我认为你已经声明了静态方法,所以你不能使用类对象来访问静态方法.您应该使用Classname来访问静态方法.

<import type="com.example.dan.bindtest.Converter"/>
Run Code Online (Sandbox Code Playgroud)

然后使用:

android:text="@={Converter.toString(first, item.dose, item.dose)}"
Run Code Online (Sandbox Code Playgroud)