Ich habe ein Textfeld, das auf ein Ansichtsmodell wie die XML hier datengebunden ist, und ich möchte, dass die Benutzeroberfläche das txtProvider-Feld bei einem Klick auf die cardView aktualisiert. Wie sollte ich die Änderung der Ansicht von meinem Viewmodel benachrichtigen?Databinding und BaseObserver - Android MVVM
<layout xmlns:bind="http://schemas.android.com/tools">
<data>
<variable name="viewmodel"
type="io.leapingwolf.myapp.viewmodel.MyViewModel"/>
<variable
name="myModel"
type="io.leapingwolf.myapp.models.MyModel"/>
</data>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cardView"
app:cardCornerRadius="5dp"
app:cardUseCompatPadding="true"
android:onClick="@{viewmodel.onClickModel}">
<TextView
android:id="@+id/txtProvider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@{myModel.provider}"
android:textColor="@android:color/black"
android:textSize="15sp" />
</android.support.v7.widget.CardView>
</layout>
In MyViewModel (der sich BaseObserver Klasse), ich habe
fun onClickModel() : View.OnClickListener {
val viewOnClick = View.OnClickListener { v ->
myModel!!.provider = "name changed"
notifyPropertyChanged(BR.myModel)
Toast.makeText(context,"clicked:" + myModel!!.provider , Toast.LENGTH_SHORT).show()}
return viewOnClick
}
Meine Daten Klasse:
data class MyModel(
val type: String?,
var provider: String?
) : AutoParcelable
Wie funktioniert Ihr Textfeld aussehen? – yennsarah
Der txtProvider in der Layoutdatei ist das Textfeld. – Akshata
Sie haben also keine Variable oder Eigenschaft für den Text in Ihrem Viewmodel ...? – yennsarah