gpt4 book ai didi

android - Spinner 2 方式数据绑定(bind)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:30:08 26 4
gpt4 key购买 nike

我无法让 2way 数据绑定(bind)与微调器一起工作。我在这里导出了我的 android studio 项目 - https://github.com/asatyana/Spinner2WayDataBinding

感谢专家帮助

这是我的 Activity 布局

<?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 中使用的适配器中有一个拼写错误。您可以解决它:

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

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

关于android - Spinner 2 方式数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39042222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com