1+ package com .vogella .android .rxjava .simple ;
2+
3+ import android .os .Bundle ;
4+ import android .support .v7 .app .AppCompatActivity ;
5+ import android .support .v7 .widget .LinearLayoutManager ;
6+ import android .support .v7 .widget .RecyclerView ;
7+ import android .view .View ;
8+ import android .widget .ProgressBar ;
9+
10+ import java .util .List ;
11+ import java .util .concurrent .Callable ;
12+
13+ import rx .Observable ;
14+ import rx .Observer ;
15+ import rx .Subscription ;
16+ import rx .android .schedulers .AndroidSchedulers ;
17+ import rx .schedulers .Schedulers ;
18+
19+ public class Example2Activity extends AppCompatActivity {
20+
21+ private Subscription mTvShowSubscription ;
22+ private RecyclerView mTvShowListView ;
23+ private ProgressBar mProgressBar ;
24+ private SimpleStringAdapter mSimpleStringAdapter ;
25+ private RestClient mRestClient ;
26+
27+ @ Override
28+ protected void onCreate (Bundle savedInstanceState ) {
29+ super .onCreate (savedInstanceState );
30+ mRestClient = new RestClient (this );
31+ configureLayout ();
32+ createObservable ();
33+ }
34+
35+ private void createObservable () {
36+ Observable <List <String >> tvShowObservable = Observable .fromCallable (new Callable <List <String >>() {
37+ @ Override
38+ public List <String > call () {
39+ return mRestClient .getFavoriteTvShows ();
40+ }
41+ });
42+
43+ mTvShowSubscription = tvShowObservable
44+ .subscribeOn (Schedulers .io ())
45+ .observeOn (AndroidSchedulers .mainThread ())
46+ .subscribe (
47+ new Observer <List <String >>() {
48+ @ Override
49+ public void onCompleted () {
50+
51+ }
52+
53+ @ Override
54+ public void onError (Throwable e ) {
55+
56+ }
57+
58+ @ Override
59+ public void onNext (List <String > tvShows ) {
60+ displayTvShows (tvShows );
61+ }
62+ });
63+ }
64+
65+ @ Override
66+ protected void onDestroy () {
67+ super .onDestroy ();
68+
69+ if (mTvShowSubscription != null && !mTvShowSubscription .isUnsubscribed ()) {
70+ mTvShowSubscription .unsubscribe ();
71+ }
72+ }
73+
74+ private void displayTvShows (List <String > tvShows ) {
75+ mSimpleStringAdapter .setStrings (tvShows );
76+ mProgressBar .setVisibility (View .GONE );
77+ mTvShowListView .setVisibility (View .VISIBLE );
78+ }
79+
80+ private void configureLayout () {
81+ setContentView (R .layout .activity_example2 );
82+ mProgressBar = (ProgressBar ) findViewById (R .id .loader );
83+ mTvShowListView = (RecyclerView ) findViewById (R .id .tv_show_list );
84+ mTvShowListView .setLayoutManager (new LinearLayoutManager (this ));
85+ mSimpleStringAdapter = new SimpleStringAdapter (this );
86+ mTvShowListView .setAdapter (mSimpleStringAdapter );
87+ }
88+ }
0 commit comments