微调2路数据绑定 [英] Spinner 2 way databinding
本文介绍了微调2路数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法获得2way数据绑定以使用微调框。我在这里出口了我的Android工作室项目 - https://github.com/asatyana/Spinner2WayDataBinding
I am not able to get 2way databinding to work with spinner. I exported my android studio project here - https://github.com/asatyana/Spinner2WayDataBinding
欣赏专家帮助
这是我的活动布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="myModel"
type="com.example.spinner.model.SpinnerModel" />
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.spinner.MainActivity"
tools:showIn="@layout/activity_main">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@{myModel.countries}"
app:selection="@{myModel.countryIdx}"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/spinner"
android:text="@{myModel.country}" />
</RelativeLayout>
</layout>
我的模型
public class SpinnerModel extends BaseObservable{
private String [] countries;
private int countryIdx;
private String country;
public SpinnerModel()
{
List<String> allCountries = new ArrayList<String>();
String[] locales = Locale.getISOCountries();
for (String countryCode : locales) {
Locale obj = new Locale("", countryCode);
allCountries.add(obj.getDisplayCountry());
}
countries = allCountries.toArray(new String[allCountries.size()]);
}
public String[] getCountries() {
return countries;
}
public void setCountries(String[] countries) {
this.countries = countries;
}
public int getCountryIdx() {
return countryIdx;
}
public void setCountryIdx(int countryIdx) {
this.countryIdx = countryIdx;
}
public String getCountry() {
return countries[countryIdx];
}
public void setCountry(String country) {
this.country = country;
}
}
推荐答案
是AS 2.1中使用的适配器中的错字。你可以解决它:
There was a typo in adapters used in AS 2.1. You can work around it:
@InverseBindingMethods({
@InverseBindingMethod(type = Spinner.class, attribute = "android:selectedItemPosition"),
})
您可以将此注释应用于任何类您的项目。
You can apply this annotation to any class in your project.
这篇关于微调2路数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文