Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

rx java - 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);
            }
        });}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...