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