1

I used some spinner binding after seeing in this answer. But i am not able to compile code after that.

Error

Found data binding errors. ****/ data binding error ****msg:Cannot find the getter for attribute 'selectedValue' with value type java.lang.String on android.widget.Spinner. file:D:\Khemraj_AndroidStudioWorkspace_\amelioFinal\app\src\main\res\layout\activity_cart.xml loc:167:24 - 175:33 ****\ data binding error ****

Pojo

public class ViewModel {
    private String text;
    public String getText() {
        return text;
    }

    public void setText(String text)
    {
       this.text = text;
    }
}

in xml

  <variable
        name="viewModel"
        type="com.amelio.model.ViewModel"/>

and

  <android.support.v7.widget.AppCompatSpinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:entries="@array/days"
            bind:selectedValue="@={viewModel.text}"/>

bindingUtil

public class SpinnerBindingUtil {

    @BindingAdapter(value = {"selectedValue", "selectedValueAttrChanged"}, requireAll = false)
    public static void bindSpinnerData(AppCompatSpinner pAppCompatSpinner, String newSelectedValue, final InverseBindingListener newTextAttrChanged) {
        pAppCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                newTextAttrChanged.onChange();
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        if (newSelectedValue != null) {
            int pos = ((ArrayAdapter<String>) pAppCompatSpinner.getAdapter()).getPosition(newSelectedValue);
            pAppCompatSpinner.setSelection(pos, true);
        }
    }
    @InverseBindingAdapter(attribute = "selectedValue", event = "selectedValueAttrChanged")
    public static String captureSelectedValue(AppCompatSpinner pAppCompatSpinner) {
        return (String) pAppCompatSpinner.getSelectedItem();
    }

}
2
  • i think the ViewModel class should have getter and setter for the property I.E. setSelectedValue() and getSelectedValue() Commented May 14, 2018 at 6:19
  • I don't think so, it is an attribute not an variable of model class. And the answer i got has 14 up-vote, strange. Commented May 14, 2018 at 6:28

1 Answer 1

4

There is an android:selectedItemPosition attribute that supports two-way data binding out of the box. You can use that or use its BindingAdapter/InverseBindingAdapter pair as a template:

@InverseBindingMethods({
    @InverseBindingMethod(type = AdapterView.class, attribute = "android:selectedItemPosition"),
    @InverseBindingMethod(type = AdapterView.class, attribute = "android:selection",
            method = "getSelectedItemPosition",
            event = "android:selectedItemPositionAttrChanged"),
})

...

@BindingAdapter("android:selectedItemPosition")
public static void setSelectedItemPosition(AdapterView view, int position) {
    if (view.getSelectedItemPosition() != position) {
        view.setSelection(position);
    }
}

@BindingAdapter(value = {"android:onItemSelected", "android:onNothingSelected",
        "android:selectedItemPositionAttrChanged" }, requireAll = false)
public static void setOnItemSelectedListener(AdapterView view, final OnItemSelected selected,
        final OnNothingSelected nothingSelected, final InverseBindingListener attrChanged) {
    if (selected == null && nothingSelected == null && attrChanged == null) {
        view.setOnItemSelectedListener(null);
    } else {
        view.setOnItemSelectedListener(
                new OnItemSelectedComponentListener(selected, nothingSelected, attrChanged));
    }
}

That would lead me to do something like this:

@InverseBindingMethods({
    @InverseBindingMethod(type = AdapterView.class,
                          attribute = "android:selectedValue",
                          method = "getSelectedItem"
})
public class SelectedValueBindingAdapter {
    @BindingAdapter("android:selectedValueAttrChanged")
    public static void setSelectedValueListener(AdapterView view,
            final InverseBindingListener attrChanged) {
        if (attrChanged == null) {
            view.setOnItemSelectedListener(null);
        } else {
            view.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    attrChanged.onChange();
                }
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    attrChanged.onChange();
                }
            });
        }
    }

    @BindingAdapter("android:selectedValue")
    public static void setSelectedValue(AdapterView<?> view, Object selectedValue) {
        Adapter adapter = view.getAdapter();
        if (adapter == null) {
            return;
        }
        // I haven't tried this, but maybe setting invalid position will
        // clear the selection?
        int position = AdapterView.INVALID_POSITION;

        for (int i = 0; i < adapter.getCount(); i++) {
            if (adapter.getItem(i) == selectedValue) {
                position = i;
                break;
            }
        }
        view.setSelection(position);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.