UI 没有使用 Rxjava 和 CompositeSubscription 进行更新

UI didn't updated using Rxjava with CompositeSubscription

我已经使用 rxjava 和 retrofit 从后端加载数据并更新 UI。 但是剖面图上没有显示数据。我已经测试过了,后端数据加载成功,可以使用假数据更新 UI 。

我使用Rxjava的时候有什么问题吗?

private void retrieveCardInfo(String stripeId, String userToken) {

      subscriptions.add(NetworkUtil.getRetrofit(userToken).getCustomerInfo(new GetCustomer(stripeId))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::handleCustomerResponse, this::handleError));
    }

    private void handleCustomerResponse(CreateStripeCustomerResponse response) {
        if (response.getSuccess()) {
            updateCardList(response);
            bankSection.setState(Section.State.EMPTY);
        } else {
            Utils.toast(this,"Get credit card failed");
        }

    }

    private void updateCardList(CreateStripeCustomerResponse response) {
        List<CardInfo> cardList = response.getCustomer().getSources().getData();
        if (cardList == null || cardList.size() == 0) {
            cardSection.setState(Section.State.EMPTY);
        } else {
            list = new ArrayList<>();
            for (int i = 0; i < cardList.size(); i++) {
                CardInfo cardInfo = cardList.get(i);
                String brand = cardInfo.getBrand();
                String subTitle = cardInfo.getFunding() + "****" + cardInfo.getLast4();

                list.add(new PaymentAccountItem(brand, subTitle, cardDrawable.get(brand)));
            }
            list.add(new PaymentAccountItem("title", "subtitle", R.drawable.ic__credit_amex_svg));
            cardSection.swipeData(list);
        }
    }

    private void handleError(Throwable throwable) {

    }

// 在使用假数据时没有 sectionedAdapter.notifyDataSetChanged(); 时工作正常,

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_payment);
        ButterKnife.bind(this);
        subscriptions = new CompositeSubscription();
        initialToolbar();
        initialRecyclerView();
        initialPaymentData();
    }

    private void initialPaymentData() {
        stripeIdAndToken = Utils.getStripeIdAndToken(this);
        if (TextUtils.isEmpty(stripeIdAndToken.first)) {
            cardSection.setState(Section.State.EMPTY);
            bankSection.setState(Section.State.EMPTY);
        } else {
            initialCardDrawableResource();
            retrieveCardInfo(stripeIdAndToken.first, stripeIdAndToken.second);
        }
// fake data here
//        initialCardDrawableResource();
//        list = new ArrayList<>();
//        list.add(new PaymentCreditCardItem("Visa", "123456", 10, 2018, cardDrawable.get("Visa")));
//        cardSection.swipeData(list);

    }

    private void initialCardDrawableResource() {
        cardDrawable = new HashMap<>();
        cardDrawable.put("Visa", R.drawable.ic_visa_svg);
        cardDrawable.put("Discover", R.drawable.ic_discover_svg);
        cardDrawable.put("American Express", R.drawable.ic__credit_amex_svg);
        cardDrawable.put("Mastercard", R.drawable.cio_ic_mastercard);
    }

    private void retrieveCardInfo(String stripeId, String token) {
        subscriptions.add(NetworkUtil.getRetrofit(token).getCustomerInfo(new GetCustomer(stripeId))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::handleCustomerResponse, this::handleError));
    }

    private void handleCustomerResponse(CreateStripeCustomerResponse response) {
        if (response.getSuccess()) {
            updateCardList(response);
        } else {
            Utils.toast(this, "Get credit card failed");
        }

    }

    private void updateCardList(CreateStripeCustomerResponse response) {
        List<CardInfo> cardList = response.getCustomer().getSources().getData();
        if (cardList == null || cardList.size() == 0) {
            cardSection.setState(Section.State.EMPTY);
        } else {
            list = new ArrayList<>();
            for (int i = 0; i < cardList.size(); i++) {
                CardInfo cardInfo = cardList.get(i);
                String brand = cardInfo.getBrand();
                String cardNum = cardInfo.getFunding() + "****" + cardInfo.getLast4();
                list.add(new PaymentCreditCardItem(brand, cardNum, cardInfo.getExpMonth(), cardInfo.getExpYear(), cardDrawable.get(brand)));
            }
            cardSection.swipeData(list);
            sectionedAdapter.notifyDataSetChanged();
        }
    }

    private void handleError(Throwable throwable) {

    }

    private void initialRecyclerView() {
        sectionedAdapter = new SectionedRecyclerViewAdapter();
        cardSection = new PaymentCardAndAccountSection(this, R.layout.header_card, R.layout.payment_card_empty_view);
        bankSection = new PaymentCardAndAccountSection(this, R.layout.header_bank, R.layout.payment_account_empty_view);
        sectionedAdapter.addSection(cardSection);
        sectionedAdapter.addSection(bankSection);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(sectionedAdapter);

        bankSection.setState(Section.State.EMPTY);
    }

    private void initialToolbar() {
        toolbar.setTitle("Payment");
        toolbar.setNavigationIcon(R.drawable.ic_back_svg);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }

    @OnClick(R.id.fab_add_payment)
    void launchAddPaymentDialog() {
        AddPaymentDialogFragment addPaymentDialogFragment = AddPaymentDialogFragment.newInstance();
        addPaymentDialogFragment.setStyle(DialogFragment.STYLE_NO_FRAME, 0);
        addPaymentDialogFragment.show(getSupportFragmentManager(), "dialog");
    }

    @Override
    public void onPause() {
        super.onPause();
        if (subscriptions != null) {
            subscriptions.clear();
        }
    }

你需要在你写 subsribeOn() 之后添加 .observeOn(AndroidSchedulers.mainThread()) 来告诉你的 observable 在 UI thread/MainThread 上执行你的 onNext 回调。

您使用适配器吗?在这种情况下 adapter.notifyDataSetChanged();

此外,打印您的错误:throwable.printStackTrace(); 以查看是否出现问题。