android - MVVM,在存储库中获取上下文,传递值
问题描述
我正在学习 MVVM 并尝试在我的存储库中获取上下文并将值从 MainActivity 传递到存储库类。
我知道您可以通过 Application 在 ViewModel 中获取上下文:
public MyViewModel(Application application) {
super(application);
...
}
myApi = RetrofitService.getClient(getApplication()).create(ApiInterface.class);
但是它对存储库类是如何工作的,我如何将字符串值(utc)从 MainActivity 传递到存储库类?
我的存储库类有以下示例代码:
private String utc:
public RetroDataRepository() {
public MutableLiveData<TimezoneModel> getTimeZones() {
final MutableLiveData<TimezoneModel> timezones = new MutableLiveData<>();
apiInterface = RetrofitService.getClient(context????).create(ApiInterface.class);
Call<ResponseBody> call = apiInterface.getTimeZone(utc);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
String time;
String utc;
String region;
String currentTime;
String timezoneTitle;
if(response.body()!=null) {
// store TimeZone data to a list
Document document = Jsoup.parse(response.body().toString());
....
}
isLoading.setValue(false);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e(TAG, "onFailure: ", t);
isLoading.setValue(false);
error.setValue(true);
}
});
return timezones;
解决方案
推荐阅读
- dockerfile - 如何在 kaniko 中为多阶段 Dockerfile 指定构建目标?
- android - 如何处理服务器连接错误和互联网连接错误并在 volley android 中为每一个显示不同的消息?
- java - java - 如何进行单元测试以断言类中是否存在属性或不使用Java中的JUnit?
- flutter - 如何在 Flutter 上添加初始值而不编辑初始值?
- javascript - JavaScript:如何使用 switch 语句显示时间?
- c++ - 有没有办法在 C++ 中为 TAB 字符(反斜杠 t)“\t”设置特定数量的最大空格?
- maven - 如何将服务器上包含的库包含到 pom 中?
- assembly - 如何更改 PWM AVR 的占空比(组装 - 无 C)
- python - ngrok 在某些特定情况下不起作用
- javascript - “autocomplete=off”属性不起作用有什么解决方案吗?