如何使用Rxjava2在会议室数据库中的Textview上显示Flowable数据 [英] How to display Flowable data on a Textview in Room Database using Rxjava2

查看:224
本文介绍了如何使用Rxjava2在会议室数据库中的Textview上显示Flowable数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DAO.class:

@Dao
public interface VisitorDAO {

    @Query("Select * from visitor")
    Flowable<List<Visitor>> getAll();

    @Insert
    Completable Insert(Visitor visitor);    //Using Single or Maybe tells the Database and the mainthread that this operation will be performed on Rxjava.

    @Update
    public void Update(Visitor visitor);

    @Delete
    public void Delete(Visitor visitor);

}

代码:

   @Override
                public void onComplete() {
                    visitorFlowable = database.visitorDAO().getAll();
                    t.setText(visitorFlowable.); //is this the right way????
                    Toast.makeText(Add_Visitors.this, "Insert Successful!", Toast.LENGTH_SHORT).show();

我已将查询设置为Flowable,其想法是访问那些Flowable返回类型数据并将其显示在textview上.

I have set the Query as Flowable and the idea is to access those Flowable return type data and display it on a textview.

推荐答案

visitorFlowable = database.visitorDAO().getAll();

将返回可流动对象,您需要在UI Thread上订阅它并更新textView

will return a flowable, you need to subscribe it on UI Thread and update textView

database.visitorDAO().getAll()
                .observeOn(schedulerProviders.ui())
                .subscribe(
                    list -> {
                        //list is List<Visitor>, use it to update textView
                    },
                    throwable -> {
                        //this block is executed if any exception is thrown
                    }
            );

这篇关于如何使用Rxjava2在会议室数据库中的Textview上显示Flowable数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆