【问题标题】:ViewModel does not get updated in the layout when setters are called from method other than onCreate()当从 onCreate() 以外的方法调用 setter 时,ViewModel 不会在布局中更新
【发布时间】:2021-12-25 07:31:59
【问题描述】:

我有一个名为 MyInfoModel 的类,其中包含一些实例变量来存储有关用户的数据和一个通知该类的观察者的方法。

private final List<ObserverInterface> observers = new ArrayList<>();
private int id;
private String name;
private String imageURL;

// Getters, Setters, Register and Unregister Methods for Instance Variables
// Also All Setter Methods call the notifyObservers() method after setting the values

private void notifyObservers(){
   Log.d(this.getClass().getName(), "Notifying All Observers");
   for (ObserverInterface observer : observers){
        observer.updateViews();
   }
}

我有一个接口ObserverInterface,它只有一个方法void updateViews();,它被实现该接口的其他类覆盖。

我正在使用 Retrofit 请求一些数据,这些数据在 Constants 类的静态对象中得到更新。

public class Constants {
   public static MyInfoModel INFO_CONSTANTS = new MyInfoModel();
}

另外,我有一个视图模型MainActivityViewModel,它扩展了ViewModel 类,并且有变量、getter 和setter 来支持活动的布局。

private int id;
private String name;
private String imageURL;

// Getters and Setters

我在这样的布局中使用了 DataBinding

<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.itsyourap.app.viewModel.MainActivityViewModel" />
    </data>

<TextView <!-- Layout Tags -->
         android:text="@{viewModel.Name}" />

然后我有MainActivity,我想在其中等待Constants.INFO_CONSTANTS 的变量值发生任何变化,所以我实现ObserverInterface 接口并重写updateViews() 方法,如下所示:-

public class MainActivity extends AppCompatActivity implements ObserverInterface {

    private MainViewModel mainViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mainViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
        mainBinding.setViewModel(mainViewModel);
        mainBinding.getViewModel().setName("Alpha Beta Gamma"); // Works
        mainBinding.getViewModel().setImageURL("somelink.jpg"); // Works
    }

    public MainActivity(){
        Constants.INFO_CONSTANTS.register(this);
    }

    @Override
    public void updateViews() {
        if (mainViewModel != null){
            Log.d(this.getClass().getName(), "Updating Views");
            mainViewModel.setName(Constants.INFO_CONSTANTS.getName()); // Does Not Work
            mainViewModel.setImageURL(Constants.INFO_CONSTANTS.getImageURL()); // Does Not Work
        }
    }
}

我面临的问题是 updateViews() 被成功调用(根据 LogCat)但布局在运行时没有更新。我尝试过使用LiveDataMutableLiveData,但它们似乎效果不佳。我也尝试使用mainBinding.setLifecycleOwner(this);,但效果不佳。

同样从onCreate() 方法调用setter 可以完美运行,但不能从updateViews() 方法调用。

我应该如何使它工作?我只想在 ViewModel 中的值更改后立即更改布局中的值。

提前致谢

【问题讨论】:

  • 为什么你在创建自定义 observable 和所有 .你不能使用 LiveData 吗?
  • 试过了!但还是不行。它在从 onCreate() 调用 setter 而不是从 updateViews() 方法调用时有效

标签: android android-viewmodel android-mvvm


【解决方案1】:

修改变量后调用setViewModel()即可解决问题

@Override
    public void updateViews() {
        if (mainViewModel != null){
            Log.d(this.getClass().getName(), "Updating Views");
            mainViewModel.setName(Constants.INFO_CONSTANTS.getName());
            mainViewModel.setImageURL(Constants.INFO_CONSTANTS.getImageURL()); 
            mainBinding.setViewModel(mainViewModel); // Solves The Problem
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    相关资源
    最近更新 更多