我正在用Android编写卡余额应用程序。用户将保存他们的卡号和卡名,然后,我需要根据卡号获取他们的余额。用户可以保存1张以上的卡。我正在使用sqlite数据库保存卡名和卡号。但是之后,我需要从服务中获取余额,然后填充实体,将其绑定到ListAdapter
并将其设置到我的ListView
中。
我的问题是从服务中获取平衡时开始的。当我循环时,当我的方法接近完成时,它将进入GetCardBalanceFromService
方法。因此,我不能使用返回数据,它将变为空。在onCreate
方法中,我将使用此ListSavedCards
方法进行列表。
这是我的代码。
protected void ListSavedCards() {
CheckTableAndCreate();
try {
ArrayList<LocalSavedCards> list = db.selectFromEntityTable(LocalSavedCards.class, null, null, null);
if (list.size() != 0) {
for (int i = 0; i < list.size(); i++) {
LocalSavedCards saved = list.get(i);
saved.setBakiye(GetCardBalanceFromService(saved.getCardNo()));
}
BindIntoListview(list);
}
} catch (Exception ex) {
Log.e(TAG, "ListSavedCards: ", ex);
}
}
protected String GetCardBalanceFromService(String alias) {
String[] cBalance = {""};
try {
restInterface = RetrofitApiClient.getClient().create(RestInterface.class);
Call<CardBalance> call = restInterface.getCardBalance(alias);
call.enqueue(new Callback<CardBalance>() {
@Override
public void onResponse(Call<CardBalance> call, Response<CardBalance> response) {
CardBalance balanceReq = response.body();
if (balanceReq != null) {
if (!balanceReq.getCardType()) {
cBalance[0] += balanceReq.getBalance() + " T1 ";
} else {
cBalance[0] += balanceReq.getBalance() + " T2 ";
}
if (!balanceReq.getWaiting().equals("0")) {
cBalance[0] += ". +" + balanceReq.getWaiting() + " T1";
}
}
}
@Override
public void onFailure(Call<CardBalance> call, Throwable t) {
cBalance[0] = "";
}
});
} catch (Exception ex) {
Crashlytics.log(ex.getMessage());
}
return cBalance[0];
}