0

我遇到了一个问题,因为我想对 numberPicker 进行数据绑定。但是我在任何地方都找不到任何示例,并且像往常一样,它在第一次尝试时不起作用……在十几个之后也不起作用:)。

所以我想质疑你的知识。

所以这基本上是我在我的 Xml 中所做的:

(我的代码的开头)

    <data>
            <variable
                name="profilmvvm"
type="com.example.dupla.sparktennis.viewmodels.ProfilViewModel"/>
            <import type="com.example.dupla.sparktennis.Services.ConversiontoInt"/>
        </data> 

在同一个文件中有点沮丧:

<NumberPicker
                        android:layout_width="50.0dp"
                        android:layout_height="100.0dp"
                        android:id="@+id/numberPickerNiveauProfil"
                        android:gravity="center_horizontal"
                        android:layout_marginLeft="10.0dp"
                        android:layout_marginTop="40.3dp"
                        android:layout_marginRight="10.0dp"
                        android:layout_marginBottom="10.0dp"
                        android:value="@{profilmvvm.niveau.niveau}"/>

这是我在 Fragment 活动中数据绑定它的方式:

 profilViewModel = new ProfilViewModel(getContext(), calendrier, rencontre, niveau, userLog, personne);

            mBinding.setProfilmvvm(profilViewModel);

这是 ViewModel :

public ProfilViewModel(Context context, CalendrierDisponibilite cal, TypeDeRencontre renc,  Niveau niv, Userslogs user, Personne pers) {

        this.context = context;
        this.calendrier = cal;
        this.niveau = niv;
        this.user = user;
        this.personne = pers;
        this.rencontre = renc;
    }


    @Bindable
    public CalendrierDisponibilite getCalendrier() {
        return calendrier;
    }

    public void setCalendrier(CalendrierDisponibilite calendrier) {
        this.calendrier = calendrier;
        notifyPropertyChanged(BR.calendrier);
    }

    @Bindable
    public TypeDeRencontre getRencontre() {
        return rencontre;
    }

    public void setRencontre(TypeDeRencontre rencontre) {
        this.rencontre = rencontre;
        notifyPropertyChanged(BR.rencontre);
    }

    @Bindable
    public Personne getPersonne() {
        return personne;
    }

    public void setPersonne(Personne personne) {
        this.personne = personne;
        notifyPropertyChanged(BR.personne);
    }

    @Bindable
    public Niveau getNiveau() {
        return niveau;
    }

    public void setNiveau(Niveau niveau) {
        this.niveau = niveau;
        notifyPropertyChanged(BR.niveau);
    }
    @Bindable
    public Userslogs getUser() {
        return user;
    }

    public void setUser(Userslogs user) {
        this.user = user;
        notifyPropertyChanged(BR.user);
    }

我是 100% 我的视图模型中有数据。我只是不确定为什么它适用于所有其他正常工作的小部件(我有很多 EditText 和 CheckBox)。

那么有什么我做的不对吗?是否可以 DataBind 一个 NumberPicker ?

谢谢你的分享:)

4

2 回答 2

0

要么使用

@Bindable 
public int getValue() {
    return niveau.niveau;
}

或通过@BindingAdapter 设置

于 2019-03-05T21:52:30.333 回答
0

我在为我的数据类中的参数设置 Number Picker 2way databindig 时遇到了类似的问题。这是适合我的解决方案:

在我的视图模型中(间隔是一个以分钟为参数的数据类):

var interval = MutableLiveData(Interval(0,-1))
var minutes = ObservableInt(interval.value!!.minutes)

fun setMinutes(newValue:Int){
    interval.value?.minutes = newValue
}

在xml中

        <NumberPicker
                android:id="@+id/number_interval_minutes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_marginEnd="10dp"
                android:timePickerMode="spinner"
                android:value="@={intervalViewModel.minutes}"
                android:onValueChange="@{(view, oldValue, newValue) -> intervalViewModel.setMinutes(newValue)}"
                app:tooltipText="Minutes"
                app:minValue="@{0}"
                app:maxValue="@{59}"
                app:textSize="@{80F}"
                app:layout_constraintEnd_toStartOf="@+id/textView4"
                app:layout_constraintTop_toBottomOf="@+id/text_input_layout_interval_title" />
enter code here
于 2021-08-13T11:33:54.057 回答