Search code examples
rx-javaobservablerx-android

AsyncTask replacement with RXJava help needed


I am new to Reactive so go easy on me, but I am trying to replace an async task that is currently run on text changes for an auto suggest function. Below is where I am at with my RX:

rest.getMemberList(etSearch.getText().toString())//rest interface
                    .subscribeOn(Schedulers.io())
                    .observeOn(RxAndroidSchedulers.mainThread())
                    .subscribe(new Action1<List<Member>>() {
                        @Override
                        public void call(List<Member> results) {
                            ListView ResultsListView = (ListView) findViewById(R.id.list);
                            MemberAdapter = new ResultRowAdapter(mContext, getLayoutInflater());
                            MemberAdapter.setMemberList(results);
                            ResultsListView.setAdapter(MemberAdapter);
                        }
                    };

So, what Im looking for is a few things: 1) am I going in the right direction with my observable? 2)how would I handle the subscription part of this? 3)any other advice related to replacing async tasks with RX because this is the first of several I need to do.

Edit:

This is where I have gotten and feel as if I am closer, but I still need guidance.etSearch is my EditText view

if(etSearch.length()>2){
        Observable<String> o1 = Observable.just(etSearch.getText().toString());

        o1.subscribeOn(Schedulers.io())
                .observeOn(RxAndroidSchedulers.mainThread()).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                List<Member> MemberList =rest.getMemberList(s);
                ListView ResultsListView = (ListView) findViewById(R.id.list);
                MemberAdapter = new ResultRowAdapter(mContext, getLayoutInflater());
                MemberAdapter.setMemberList(MemberList);
                ResultsListView.setAdapter(MemberAdapter);
            }
        });}

Solution

  • This is how I did it:

    Observable<String> o1 = RxTextView.textChangeEvents(etSearch)
                    .map(new Func1<RxTextViewTextChangeEvent, String>() {
                             @Override
                             public String call(RxTextViewTextChangeEvent rxTextViewTextChangeEvent) {
                                 return etSearch.getText().toString();
                             }
                         }
                    );
    
                            o1.observeOn(RxAndroidSchedulers.mainThread()).filter(new Func1<String, Boolean>() {
                                @Override
                                public Boolean call(String s) {
                                    return s.length()>2;
                                }
                            }).subscribe(new Action1<String>() {
                                @Override
                                public void call(String s) {
                                    if (s.length() > 2) {
                                        List<Member> MemberList = rest.getMemberList(s);
                                        ListView ResultsListView = (ListView) findViewById(R.id.list);
                                        MemberAdapter = new ResultRowAdapter(mContext, getLayoutInflater());
                                        MemberAdapter.MemberList(MemberList);
                                        ResultsListView.setAdapter(MemberAdapter);
                                    }
                                }
                            });