我無法獲得2way數據綁定與微調工作。我出口在這裏我的Android Studio項目 - https://github.com/asatyana/Spinner2WayDataBindingSpinner 2 way databinding
鑑賞專家幫助
這裏是我的活動佈局
<?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;
}
}
您正在使用哪個版本的Android Studio? –
對不起,我會多說一句:對BindingAdapter有一個(簡單的)修復會影響到spinners,但這個修復只是在Android Studio 2.2的更高版本中默認的。 –
仍然在2.1,因爲這是最新的穩定。讓我嘗試升級到2.2測試版。 – Ananth