2016-08-19 81 views
4

我无法获得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; 
    } 
} 
+0

您正在使用哪个版本的Android Studio? –

+0

对不起,我会多说一句:对BindingAdapter有一个(简单的)修复会影响到spinners,但这个修复只是在Android Studio 2.2的更高版本中默认的。 –

+0

仍然在2.1,因为这是最新的稳定。让我尝试升级到2.2测试版。 – Ananth

回答

6

有在适配器中AS使用的错别字2.1。你可以解决它:

@InverseBindingMethods({ 
    @InverseBindingMethod(type = Spinner.class, attribute = “android:selectedItemPosition”), 
}) 

您可以在项目中应用此批注任何类。

+0

我得到@InverseBindingMethods不适用于AS 2.1.3的方法 –

+0

因为它应该应用于类,而不是方法。 – Ph0en1x

+0

非常感谢乔治,我能够让它工作。用我的例子更新了github。 – Ananth