【问题标题】:Spinner 2 way databindingSpinner 2 路数据绑定
【发布时间】:2016-08-19 15:01:18
【问题描述】:

我无法让 2way 数据绑定与微调器一起使用。我在这里导出了我的 android studio 项目 - 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;
    }
}

【问题讨论】:

  • 您使用的是哪个版本的 Android Studio?
  • 抱歉,我会说更多:对 BindingAdapter 有一个(简单)修复会影响微调器,但该修复仅在更高版本的 Android Studio 2.2 中默认。
  • 仍然是 2.1,因为这是最新的稳定版本。让我尝试升级到 2.2 BETA。
  • 我使用相同的方法来解决微调器的 2 路数据绑定问题,我发现 selectedItemPosition 上有警告。为什么会这样。

标签: android data-binding


【解决方案1】:

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

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

您可以将此注释应用到项目中的任何类。

【讨论】:

  • 我得到 @InverseBindingMethods 不适用于 AS 2.1.3 的方法
  • 因为它应该应用于类,而不是方法。
  • 非常感谢乔治,我能够让它工作。用我的例子更新了 github。
猜你喜欢
  • 2017-11-05
  • 1970-01-01
  • 1970-01-01
  • 2020-03-27
  • 2018-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-15
相关资源
最近更新 更多