How to display Flowable data on a Textview in Room Database using Rxjava2

拜拜、爱过 提交于 2020-03-25 16:06:30

问题


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

}

Code:

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

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


回答1:


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

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


来源:https://stackoverflow.com/questions/60587478/how-to-display-flowable-data-on-a-textview-in-room-database-using-rxjava2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!