rx代替AsyncTask

rx1.2.1

public class InvoiceUtils {
    public static Observable<Invoice> getLocalInvoice(final Context context) {
        return Observable.create(new Observable.OnSubscribe<Invoice>() {
            @Override
            public void call(Subscriber<? super Invoice> subscriber) {
                subscriber.onNext(getInvoice(context));
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    private static Invoice getInvoice(Context context) {
        Cursor cursor = DbHelper.getInstance(context).query(TInvoice.TNAME, " where " + TInvoice.ACCOUNT + "='" + UserInfoKeeper.getCurrentUser().getAccount() + "'");
        if (cursor.getCount() == 0) {
            return null;
        }

        cursor.moveToNext();

        Invoice invoice = new Invoice();
        invoice.setInvoiceTitle(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICETITLE)));
        invoice.setInvoiceBandAccount(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICEBANDACCOUNT)));
        invoice.setInvoiceFlag(cursor.getInt(cursor.getColumnIndex(TInvoice.INVOICEFLAG)));
        invoice.setInvoiceBandName(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICEBANDNAME)));
        invoice.setInvoiceToAddress(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICETOADDRESS)));
        invoice.setInvoiceToTel(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICETOTEL)));
        invoice.setInvoiceToContact(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICETOCONTACT)));
        invoice.setInvoiceFromAddress(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICEFROMADDRESS)));
        invoice.setInvoiceFromTel(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICEFROMTEL)));
        invoice.setInvoiceType(cursor.getInt(cursor.getColumnIndex(TInvoice.INVOICETYPE)));
        invoice.setInvoiceIdentifyId(cursor.getString(cursor.getColumnIndex(TInvoice.INVOICEIDENTIFYID)));

        cursor.close();

        return invoice;
    }


}

要使用的地方:

InvoiceUtils.getLocalInvoice(this).subscribe(new Subscriber<Invoice>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Invoice invoice) {
                if (invoice != null) {
                    if (!TextUtils.isEmpty(invoice.getInvoiceTitle())) {
                        if (invoice != null) {
                            setViews(invoice);
                        } else {
                            // 本地为空,去网络获取,存于本地
                            presenter.getInvoiceInfo();
                        }
                    }
                }
            }
        });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值