我有一个叫做"Item"的对象。
现在我正在尝试在我的activity_main.xml中的EditText中使用这个:
如果我从EditText中删除android:text=...这一行,它会编译通过而没有错误。另外,我已经尝试移除@Bindable注解和notifyPropertyChanged(BR.dose)行,但仍然遇到相同的问题。如果有人想检查,我在一个公共代码库中放置了此代码:https://github.com/danponce/bindtest 有人有线索吗?@GeorgeMount ?
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);
}
}
现在按照上一篇@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;
}
}
现在我正在尝试在我的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>
但问题在于,即使我按照步骤一步一步地操作,我仍然遇到了这个错误:
"Error:cannot generate view binders java.lang.ClassCastException: android.databinding.tool.expr.IdentifierExpr cannot be cast to android.databinding.tool.expr.StaticIdentifierExpr"
如果我从EditText中删除android:text=...这一行,它会编译通过而没有错误。另外,我已经尝试移除@Bindable注解和notifyPropertyChanged(BR.dose)行,但仍然遇到相同的问题。如果有人想检查,我在一个公共代码库中放置了此代码:https://github.com/danponce/bindtest 有人有线索吗?@GeorgeMount ?