Android MVVM模型中的数据绑定示例中未更新数据-在模型之间更新数据

Android MVVM模型中的数据绑定示例中未更新数据-在模型之间更新数据,android,mvvm,android-databinding,android-mvvm,Android,Mvvm,Android Databinding,Android Mvvm,我试图在下面简单的MVVM代码中实现双向数据绑定。它在文本视图中以字符串“Alex”硬编码显示“Alex”,后来我用字符串“Ramone”更改了editview。但是editview中的更改没有反映在textview中。我已经分享了下面的代码,请帮助修复相同的 更新: public void setTextMsg(String text){ db.setTextMsgModel(text); this.price = text; notifyPro

我试图在下面简单的MVVM代码中实现双向数据绑定。它在文本视图中以字符串“Alex”硬编码显示“Alex”,后来我用字符串“Ramone”更改了editview。但是editview中的更改没有反映在textview中。我已经分享了下面的代码,请帮助修复相同的

更新:

public void setTextMsg(String text){
        db.setTextMsgModel(text);
        this.price = text;
        notifyPropertyChanged(BR.ViewModel);
    }

    @Bindable
    public String getTextMsg(){
        return db.getTextMsgModel();
    }
ListViewModel:

DB:

主要活动:

activity_main.xml:



更改的位置
TextView
值。show related code public void setTextMsg(String text){db.setTextMsgModel(text);this.price=text;notifyPropertyChanged(BR.ViewModel);}@Bindable public String getTextMsg(){return db.getTextMsgModel();}我想这是由于您的代码notifyPropertyChanged(BR.ViewModel);在setTextMsg方法中。您应该通知正确的属性,在这种情况下,应该改为notifyPropertyChanged(BR.TextMsg)。请尝试一下,如果有效,请反馈。谢谢
package com.example.newmvvm.listviewmodel;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.lifecycle.ViewModel;

import com.example.newmvvm.BR;
import com.example.newmvvm.Model.DB;

public class ListViewModel extends BaseObservable {
    DB db;
    String price;

    public ListViewModel(){
        db = new DB("Alex");
    }

    public void setTextMsg(String text){
        db.setTextMsgModel(text);
        this.price = text;
        notifyPropertyChanged(BR.ViewModel);
    }

    @Bindable
    public String getTextMsg(){
        return db.getTextMsgModel();
    }

}
package com.example.newmvvm.Model;

public class DB {
    private String textMsgModel = null;

    public DB(String text){
        this.textMsgModel = text;
    }
    public void setTextMsgModel(String text){
        this.textMsgModel = text;
        //return text;
    }
    public String getTextMsgModel(){
        return this.textMsgModel;
    }
}
package com.example.newmvvm.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;

import android.os.Bundle;

import com.example.newmvvm.listviewmodel.ListViewModel;
import com.example.newmvvm.R;
import com.example.newmvvm.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        activityMainBinding.setViewModel(new ListViewModel());
        //ListViewModel viewModel = ViewModelProviders.of(this).get(ListViewModel.class);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >
    <data>
        <variable
            name="ViewModel"
            type="com.example.newmvvm.listviewmodel.ListViewModel"
            />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".View.MainActivity">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:text="@={ViewModel.TextMsg}" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText"
            android:text="Click Me" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{ViewModel.TextMsg}" />

    </LinearLayout>
</layout>