我是Reactive的新手,所以对我很轻松,但我正在尝试替换当前在自动建议功能的文本更改上运行的异步任务。以下是我在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);
}
};
所以,我正在寻找的是一些东西: 1)我的观察力是否朝着正确的方向发展? 2)我将如何处理此订阅部分? 3)与用RX替换异步任务相关的任何其他建议,因为这是我需要做的几件事中的第一件。
编辑:
这是我得到的地方,感觉好像离我更近了,但我仍然需要指导。我的编辑是我的EditText视图
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);
}
});}
答案 0 :(得分:3)
这就是我做的。谢谢你没有StackOverflow哈哈:
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);
}
}
});