Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android data binding two way Conversion methods

I have this object called 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);
    }
}

Now following last tutorial from @GeorgeMount here: https://medium.com/google-developers/android-data-binding-inverse-functions-95aab4b11873 I added the Converter class:

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;
    }
}

Now I'm trying to use this in my EditText in my activity_main.xml:

<?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>

But the problem with this is that even when I follow step by step the instructions I'm having this error:

"Error:cannot generate view binders java.lang.ClassCastException: android.databinding.tool.expr.IdentifierExpr cannot be cast to android.databinding.tool.expr.StaticIdentifierExpr"

If I remove the android:text=... line from the EditText it compiles with no errors. Also, I've tried to remove the @Bindable annotation and notifyPropertyChanged(BR.dose) lines and still has the same problem. I have this code in a public repository if anyone would want to check it: https://github.com/danponce/bindtest Anybody has a clue? @GeorgeMount ?

like image 684
Dan Ponce Avatar asked Jan 04 '23 13:01

Dan Ponce


1 Answers

I think you've declared static method so you can't use a class object to access the static methods. you should use Classname to access static methods.

<import type="com.example.dan.bindtest.Converter"/>

and then use :

android:text="@={Converter.toString(first, item.dose, item.dose)}"
like image 179
Urvish rana Avatar answered Jan 12 '23 00:01

Urvish rana