我正在学习RxAndroid api,所以我创建了一个示例,其中输入是两个整数,输出应该是包含这些整数的字符串。例如, 对于1和2,输出应该是" 1,2"等等。
以下代码中的是我尝试实现我计划做的事情,但我需要指导和帮助。
码:
public class MainActivity extends AppCompatActivity {
private final static String TAG = MainActivity.class.getSimpleName();
private EditText mEditTextValue1 = null;
private EditText mEditTextValue2 = null;
private Button mButtonStartAsyncTask = null;
private rx.Observable<Integer> mAsyncObservable = null;
private TextView mTextViewProcessedValue = null;
Subscriber<String> mAsyncSubscriber = new Subscriber<String>() {
@Override
public void onCompleted() {
Log.w(TAG, "onCompleted(mAsyncSubscriber)");
}
@Override
public void onError(Throwable e) {
Log.w(TAG, "onError(mAsyncSubscriber)");
}
@Override
public void onNext(String next) {
Log.w(TAG, "onNext(mAsyncSubscriber)");
mTextViewProcessedValue.setText();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
mEditTextValue1 = (EditText) findViewById(R.id.editTextValue1);
mEditTextValue2 = (EditText) findViewById(R.id.editTextValue2);
mButtonStartAsyncTask = (Button) findViewById(R.id.buttonStartAsyncTask);
mButtonStartAsyncTask.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer value1 = Integer.valueOf(mEditTextValue1.getText().toString());
Integer value2 = Integer.valueOf(mEditTextValue2.getText().toString());
mAsyncObservable = rx.Observable.just(value1, value2)
.delay(3, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.doOnNext(items-> {
mTextViewProcessedValue = (TextView) findViewById(R.id.textViewProcessedValue);
return value1 + ", " + value2;
})
.doOnCompleted(new Action0() {
@Override
public void call() {
Log.w(TAG, "doOnCompleted");
}
})
//.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((Action1<? super Integer>) mAsyncSubscriber);
}
});
}
}
答案 0 :(得分:0)
我也是RxJava的新手......这可能是你想要的
io.reactivex.Observable.range(1, 100).subscribe(new DisposableObserver<Integer>() {
List<Integer> list = new ArrayList<>(2);
@Override
public void onNext(Integer integer) {
list.add(integer);
if (list.size() % 2 == 0) {
Log.i(TAG, "onNext: couple int :" + list.get(0) + " ," + list.get(1));
list.clear();
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
如果你想一起发出两个值,你可以发出一个数组或列表或Pojo。
使用zip operator示例:
io.reactivex.Observable<Integer> s1 = io.reactivex.Observable.range(1, 100);
io.reactivex.Observable<Integer> s2 = io.reactivex.Observable.range(-100, 100);
io.reactivex.Observable.zip(s1, s2, new BiFunction<Integer, Integer, String >() {
@Override
public String apply(Integer i1, Integer i2) throws Exception {
return i1 + " ," + i2;
}})
.subscribe(System.out::println);
答案 1 :(得分:0)
您想要使用的操作员实际上取决于您将拥有多少个数字,您想要多少产品,以及您想要对最终答案做什么。对于此示例,我们将使用scan(...)
将函数应用于Observable发出的每个项目,按顺序,并发出每个连续值
Observable.just(value1, value2)
.delay(3, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.scan("", (string, integer) -> string + "," + integer)
上面会发出一次:"{value1},{value2}"
。
如果您添加第三个值,它将发出两次:"{value1},{value2}"
和"{value1},{value2},{value3}"
答案 2 :(得分:0)
在代码中查看您尝试实现的内容是在按钮单击时从2个字段读取并将它们发送到可观察状态,是否正确?
请检查此项目以使用rxjava绑定替换侦听器: https://github.com/JakeWharton/RxBinding。那么代码就是这样的:
RxView.clicks(button)
.map(event -> {
Integer value1 = Integer.valueOf(mEditTextValue1.getText().toString());
Integer value2 = Integer.valueOf(mEditTextValue2.getText().toString());
return value1 + "," + value2;
})
.subscribe(combinedIntegers ->
mTextViewProcessedValue.setText(combinedIntegers));