androidandroid-databindingandroid-number-picker

Databinding with NumberPicker


I meet a problem, because I want to databind a numberPicker. But I ca't find any example anywhere , and as usual, it doesn't work at the first try ... neither after a dozen :) .

So I would like to question your knowledge.

So here is basically what I've done in my Xml :

(Beginning of my code)

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

Here is a bit downer in the same file :

<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}"/>

Here is now the way I databind it in the Fragment activity :

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

            mBinding.setProfilmvvm(profilViewModel);

and here is the 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);
    }

I am 100% I have data in my viewmodel. I am just not sure why it works for every other widget (I have many EditText and CheckBox) that works correctly.

So is there something I don't do correctly ? Is it possible to DataBind a NumberPicker ?

Thank you for you thoughfull shares :)


Solution

  • Either use

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

    or set it via @BindingAdapter