循环Sqlite表并从Android Java服务中请求一些数据

时间:2019-07-09 07:20:13

标签: java android

我正在用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];
}

0 个答案:

没有答案