从 RxAndroid 访问项目布局?
Access to items layout from RxAndroid?
我正在使用下面的代码:
mCompositeDisposable.add(myObservable(IdLang, shortTermCoursesModels)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String string) {
listenerCoursesListItemService.adapterHideProgress();
}
}));
并且:
private Observable<String> myObservable(String IdLang, List<GetShortTermCoursesModel> shortTermCoursesModels) {
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> call() throws Exception {
listenerCoursesListItemService.adapterShowProgress();
for (int i = 0; i < 900000; i++) {
Log.i("WWWWWWWW", "A " + i);
}
return Observable.just("ok");
}
});
但是给我以下错误:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
如何从 RxAndroid
.
访问 UI
我的错误在这里:
listenerCoursesListItemService.adapterShowProgress();
listenerCoursesListItemService.adapterHideProgress();
你必须在主线程上订阅:
mCompositeDisposable.add(myObservable(IdLang, shortTermCoursesModels)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String string) {
listenerCoursesListItemService.adapterHideProgress();
}
}));
原因是您试图显示进度 ->
listenerCoursesListItemService.adapterShowProgress();
实际上在不同线程 (Schedulers.io()) 而不是 UI 线程 (AndroidSchedulers.mainThread()) 上运行的内部 call() 方法。
相反,您可以在 doOnSubscribe() 方法中显示进度并在 onComplete() 中关闭它。这两种方法都将在 UI 线程上调用。更新了下面的代码以供参考,
mCompositeDisposable.add(myObservable(IdLang, shortTermCoursesModels)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
listenerCoursesListItemService.adapterShowProgress();
}
})
.doFinally(new Action() {
@Override
public void run() throws Exception {
listenerCoursesListItemService.adapterHideProgress();
}
})
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String string) {
}
}));
并且:
private Observable<String> myObservable(String IdLang, List<GetShortTermCoursesModel> shortTermCoursesModels) {
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> call() throws Exception {
for (int i = 0; i < 900000000; i++) {
Log.i("WWWWWWWW", "A " + i);
}
return Observable.just("ok");
}
});
}
在 onNext() 中,您将从 call() 方法中获取 return 的字符串。
我正在使用下面的代码:
mCompositeDisposable.add(myObservable(IdLang, shortTermCoursesModels)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String string) {
listenerCoursesListItemService.adapterHideProgress();
}
}));
并且:
private Observable<String> myObservable(String IdLang, List<GetShortTermCoursesModel> shortTermCoursesModels) {
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> call() throws Exception {
listenerCoursesListItemService.adapterShowProgress();
for (int i = 0; i < 900000; i++) {
Log.i("WWWWWWWW", "A " + i);
}
return Observable.just("ok");
}
});
但是给我以下错误:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
如何从 RxAndroid
.
UI
我的错误在这里:
listenerCoursesListItemService.adapterShowProgress();
listenerCoursesListItemService.adapterHideProgress();
你必须在主线程上订阅:
mCompositeDisposable.add(myObservable(IdLang, shortTermCoursesModels)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String string) {
listenerCoursesListItemService.adapterHideProgress();
}
}));
原因是您试图显示进度 ->
listenerCoursesListItemService.adapterShowProgress();
实际上在不同线程 (Schedulers.io()) 而不是 UI 线程 (AndroidSchedulers.mainThread()) 上运行的内部 call() 方法。
相反,您可以在 doOnSubscribe() 方法中显示进度并在 onComplete() 中关闭它。这两种方法都将在 UI 线程上调用。更新了下面的代码以供参考,
mCompositeDisposable.add(myObservable(IdLang, shortTermCoursesModels)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
listenerCoursesListItemService.adapterShowProgress();
}
})
.doFinally(new Action() {
@Override
public void run() throws Exception {
listenerCoursesListItemService.adapterHideProgress();
}
})
.subscribeWith(new DisposableObserver<String>() {
@Override
public void onComplete() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(String string) {
}
}));
并且:
private Observable<String> myObservable(String IdLang, List<GetShortTermCoursesModel> shortTermCoursesModels) {
return Observable.defer(new Callable<ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> call() throws Exception {
for (int i = 0; i < 900000000; i++) {
Log.i("WWWWWWWW", "A " + i);
}
return Observable.just("ok");
}
});
}
在 onNext() 中,您将从 call() 方法中获取 return 的字符串。