问题
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