如何解决MutableLiveData值未反映在View类中
我正在通过数据绑定实现MVVM模式。问题在于MutableLiveData值未反映在View类中。我的视图课如下:
public class SettingsFragment extends Fragment {
SettingsViewModel settingsViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
SettingsRepository repository = new SettingsRepository(getContext());
SettingsFactory factory = new SettingsFactory(repository);
FragmentSettingsBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_settings,container,false);
settingsViewModel = new ViewModelProvider(this,factory).get(SettingsViewModel.class);
binding.setViewModel(settingsViewModel);
return binding.getRoot();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
settingsViewModel.getFeedbackResponse().observe(getActivity(),response -> {
Log.e("inside",response.getMessage());
});
}}
我正在发布一个Log Inside视图类,但是它从未被调用。
我的 viewModel 类如下:
public class SettingsViewModel extends ViewModel {
public static String SWITCH_KEY = "SWITCH_KEY";
SettingsRepository repository;
MutableLiveData<Feedback> feedbackMutableLiveData;
public SettingsViewModel(SettingsRepository repository) {
this.repository = repository;
feedbackMutableLiveData = new MutableLiveData<>();
}
//show feedback form
public void onFeedbackClicked(View view) {
AppUtil.showFeedbackDialog(view.getContext(),new AppUtil.FeedbackListener() {
@Override
public void onSubmitFeedback(FeedbackBody feedbackBody) {
Log.e("inside","response");
feedbackMutableLiveData = repository.sendFeedback(feedbackBody);
}
});
}
public LiveData<Feedback> getFeedbackResponse() {
return feedbackMutableLiveData;
}}
我的存储库类别是:
public class SettingsRepository extends SafeApiRequest {
MyApi myApi;
Context context;
public SettingsRepository(Context context) {
SetupRetrofit setupRetrofit = new SetupRetrofit(context);
this.myApi = setupRetrofit.createService(MyApi.class);
}
public MutableLiveData<Feedback> sendFeedback(FeedbackBody feedbackBody) {
return callRetrofitObjectResponse(context,myApi.sendFeedback("feedback/store",feedbackBody));
}
}
SafeAPiRequest 类为:
public class SafeApiRequest {
public static <T> MutableLiveData<T> callRetrofitObjectResponse(Context context,Call<T> call) {
MutableLiveData<T> responseObject = new MutableLiveData<>();
Log.e("inside",call.request().url() + "------");
call.enqueue(new Callback<T>() {
@Override
public void onResponse(Call<T> call,Response<T> response) {
Log.e("inside","successfull");
responseObject.setValue(response.body());
}
@Override
public void onFailure(Call<T> call,Throwable t) {
}
});
return responseObject;
}
}
当用户单击反馈按钮时,将调用onFeedbackClicked
方法。在此方法内部,一旦用户单击“提交”按钮,就会调用覆盖方法onSubmitFeedback
,其中feedbackMutableLiveData
被分配给api调用,该API返回的值是mutablelivedata。但是主要的问题是它的值没有反映在视图类中。我尝试在viewmodel类的构造函数中删除其值的初始化,但是在执行此操作时,它会在我们观察的位置给出NullPointerException
。
当我尝试在 view 类中获取feedbackMutableLiveData
的值时,这种情况下可能的解决方案是什么。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。