gpt4 book ai didi

android - 将数据从存储库传递到 ViewModel

转载 作者:行者123 更新时间:2023-12-02 16:52:06 24 4
gpt4 key购买 nike

我需要将速率变量从我的存储库传递到 View 模型。在 View 模型中,我有calculateRate方法,单击按钮后我将货币获取到改造问题,然后改造从网络获取数据。在汇率变量中,我有两种货币的兑换,我需要将其传递给 View 模型。怎么做?

存储库

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;
}

}

View 模型

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();
}
}

完成了!

您现在可以观察 View ModelgetRate() 方法,并根据观察者的 onChange() 中的值使用react> 方法。

关于android - 将数据从存储库传递到 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54090653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com