私はここでxmlのようなビューモデルにデータバインドされているテキストフィールドを持っており、UIはcardViewのクリック時にtxtProviderフィールドを更新します。 viewmodelからビューへの変更をどのように通知する必要がありますか? (BaseObserverクラスを拡張)MyViewModelでDatabinding and 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>
、私は
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
}
私のデータクラスを持っている:
data class MyModel(
val type: String?,
var provider: String?
) : AutoParcelable
を得ることができない場合テキストフィールドのように見える? – yennsarah
レイアウトファイルのtxtProviderはテキストフィールドです。 – Akshata
あなたのビューモデルのテキストの変数またはプロパティがありません...? – yennsarah