Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BindingAdapter custom getter 2 way data binding

I've created a custom setter that sets the text of an enum into an editText:

public enum Gender {
MALE, FEMALE}

XML:

 <EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    custom:holder="@={user.gender}" />

Adapter:

@BindingAdapter ("custom:holder")
public static void setHolder(EditText editText, Gender gender) {
    editText.setText(gender.toString());
}

From code:

binder= DataBindingUtil.bind(getView());
binder.setUser(user);

How can I specify a getter that returns a Gender object?

I tried the following code but it doesn't seem to be the right way.

@BindingAdapter ("custom:holder")
public static Gender getHolder(EditText editText) {
    return Gender.valueOf(editText.getText().toString());
}

What I want to achieve is that when I call binder.getUser(), the Gender will be automatically filled. Is it possible to do that?

like image 369
Manza Avatar asked Jan 24 '17 11:01

Manza


1 Answers

Instead of using EditText for enum value, I suggest to use Spinner with implementations of @BindingAdapter and @InverseBindingAdapter for bidirection binding.

Your dto:

public class User {
    private Gender _gender;
    public Gender getGender() { return _gender; }
    public void setGender(Gender gender) { _gender = gender; }
}

public enum Gender {MALE, FEMALE}

Implementation of @BindingAdapter for Spinner:

@BindingAdapter (value = {"custom:holder", "custom:holderAttrChanged"}, requireAll = false)
public static void setHolder(Spinner spinner, Gender gender, final InverseBindingListener listener) {
    // setup spinner content
    spinner.setAdapter(new ArrayAdapter<>(spinner.getContext(), R.layout.gender_spinner_item, Gender.values()));

    // setup listener of notifications to @InverseBindingAdapter
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            listener.onChange();
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            listener.onChange();
        }
    });
}

where gender_spinner_item - it's can be layout with one TextView.

Implementation of @InverseBindingAdapter:

@InverseBindingAdapter(attribute = "custom:holder", event = "custom:holderAttrChanged")
public static Gender getHolder(Spinner spinner) {
    return (Gender) spinner.getSelectedItem();
}

Usage:

Layout example:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:custom="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="user" type="com.example.User"/>
    </data>

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        custom:holder="@={user.gender}" />

</layout>

Binding instance of User:

binder= DataBindingUtil.bind(getView());
binder.setUser(user);
like image 69
Sergey Bubenshchikov Avatar answered Oct 18 '22 01:10

Sergey Bubenshchikov