我需要将速率变量从我的存储库传递到视图模型。 在视图模型中,我有calculateRate方法,单击按钮后我将货币获取到改造问题,然后改造从网络获取数据。在汇率变量中,我有两种货币的兑换,我需要将其传递给视图模型。怎么办?
存储库
public class CurrencyRepository {
private ApiInterface apiInterface;
private String apiKey = ApiClient.KEY;
private String TAG = "REPOSITORY ";
public CurrencyRepository(Application application) {
apiInterface = ApiClient.getClient();
}
public LiveData<Currency> getCurrency(String base, final String target, String apiKey) {
final MutableLiveData<Currency> data = new MutableLiveData<>();
apiInterface.getCurrentCurrency(base, target, apiKey).enqueue(new Callback<Currency>() {
@Override
public void onResponse(Call<Currency> call, Response<Currency> response) {
data.setValue(response.body());
String get = data.getValue().getTarget();
double rate = 0;
DecimalFormat df = new DecimalFormat("0.00"); //#
switch (get) {
case "EUR":
rate = data.getValue().getRates().getEUR();
break;
case "PLN":
rate = data.getValue().getRates().getPLN();
break;
case "USD":
rate = data.getValue().getRates().getUSD();
break;
case "CHF":
rate = data.getValue().getRates().getCHF();
break;
}
Log.d(TAG, String.valueOf(df.format(rate)));
}
@Override
public void onFailure(Call<Currency> call, Throwable t) {
}
});
return data;
}
}
查看模型
public class MainViewModel extends AndroidViewModel {
private CurrencyRepository currencyRepository;
public final ObservableField<String> from = new ObservableField<>();
public final ObservableField<String> to = new ObservableField<>();
public final ObservableFloat value = new ObservableFloat();
public final ObservableField<String> result = new ObservableField<>();
public MainViewModel(Application application) {
super(application);
currencyRepository = new CurrencyRepository(application);
}
public void calculateRate() {
currencyRepository.getCurrency(String.valueOf(from.get()), String.valueOf(to.get()), ApiClient.KEY);
}
}
你可以这样做:
将
rate
定义为 LiveData
字段而不是局部变量:
public class CurrencyRepository {
private ApiInterface apiInterface;
private String apiKey = ApiClient.KEY;
private String TAG = "REPOSITORY ";
private MutableLiveData<Double> rate = new MutableLiveData(); //or new it in constructor
....
然后在您的服务调用方法中您需要执行以下操作:
public MutableLiveData<Currency> getCurrency(String base, final String target, String apiKey) {
final MutableLiveData<Currency> data = new MutableLiveData<>();
apiInterface.getCurrentCurrency(base, target, apiKey).enqueue(new Callback<Currency>() {
@Override
public void onResponse(Call<Currency> call, Response<Currency> response) {
data.setValue(response.body());
String get = data.getValue().getTarget();
DecimalFormat df = new DecimalFormat("0.00"); //#
switch (get) {
case "EUR":
rate.postValue(data.getValue().getRates().getEUR());
break;
case "PLN":
rate.postValue(data.getValue().getRates().getPLN());
break;
case "USD":
rate.postValue(data.getValue().getRates().getUSD());
break;
case "CHF":
rate.postValue(data.getValue().getRates().getCHF());
break;
}
Log.d(TAG, String.valueOf(df.format(rate)));
}
@Override
public void onFailure(Call<Currency> call, Throwable t) {
}
});
return data;
}
在您的存储库中添加
getter
表示速率:
public LiveData<Double> getRate() {
return rate;
}
在您的
ViewModel
中,您需要一种从存储库获取rate
的方法:
public class MainViewModel extends ViewModel {
...
public LiveData<Double> getRate(){
return currencyRepository.getRate();
}
}
就这样完成了!
您现在可以观察
getRate()
的 View Model
方法,并根据观察者的 onChange()
方法中的值做出反应。
使用 MutableLiveData 并不是将数据从存储库传递到视图模型的好主意,您可以使用回调