Как сделать двухстороннее связывание данных в блесне с помощью viewmodel в android? - PullRequest
0 голосов
/ 13 февраля 2020

Я получил ответы, что двустороннее связывание данных для счетчика выполняется путем расширения BaseObservable , но как сделать это путем расширения ViewModel ?
Я нашел эту ссылку, но я не сделал не понимаю и не работает https://code.luasoftware.com/tutorials/android/android-two-way-data-binding-with-spinner/

@InverseBindingMethods({
    @InverseBindingMethod(type = Spinner.class, attribute = "android:selectedItemPosition"),
})
public class Model extends BaseObservable {

private String[] countries;
private int position;
private String country;


public Model() {
    List<String> allCountries = new ArrayList<>();
    String[] locales = Locale.getISOCountries();

    for (String countryCode : locales) {

        Locale obj = new Locale("", countryCode);

        allCountries.add(obj.getDisplayCountry());

    }

    countries = allCountries.toArray(new String[allCountries.size()]);
}

@Bindable
public String[] getCountries() {
    return countries;
}

public void setCountries(String[] countries) {
    this.countries = countries;
    notifyPropertyChanged(com.example.spinner.BR.countries);
}

@Bindable
public int getPosition() {
    return position;
}

public void setPosition(int position) {
    this.position = position;

    //if (position != 0)
    setCountry(countries[position]);
}

@Bindable
public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
    notifyPropertyChanged(com.example.spinner.BR.country);
}

public int getPosition(Spinner spinner) {
    return spinner.getSelectedItemPosition();
}
}
...