Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxAndroid textview events called automatically before text change events

I used rxandroid for debounce operation on an edittext search

I used

private void setUpText() {
        _mSubscription = RxTextView.textChangeEvents(searchStation)//
                .debounce(500, TimeUnit.MILLISECONDS)// default Scheduler is Computation
                .observeOn(AndroidSchedulers.mainThread())//
                .subscribe(getOps().getdata());
    }

and observer as

public Observer<TextViewTextChangeEvent> getdata()
    {

        return new Observer<TextViewTextChangeEvent>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                 e.printStackTrace();
            }

            @Override
            public void onNext(TextViewTextChangeEvent onTextChangeEvent) {
//                stationsugession(onTextChangeEvent.text().toString());

                //here i called link to get the data from the server
            }
        };
    }

My problem is the link is called even before any edittext changes occurs. And its not calling the textchange events. Am i missing something What am i doing wrong here. I am new to rxandroid and rxjava.

I used

  compile 'io.reactivex:rxandroid:1.1.0'
    compile 'io.reactivex:rxjava:1.1.0'
    compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'

EDIT:

It now works now, i was getting a null pointer in my logic for getting list.. when i used the onError method and put a stacktrace i found the problem.

And if you want to skip the initial call then we should call .skip(1) to your subscription object. [thanks to Daniel Lew ]

The above code is working perfectly now

like image 645
George Thomas Avatar asked Dec 21 '25 06:12

George Thomas


1 Answers

RxTextView.textChanges() emits the current text value of the TextView before emitting any changes. See the documentation.

If you want to only debounce the changes, then you should add skip(1), which will ignore the initial emission:

_mSubscription = RxTextView.textChangeEvents(searchStation)
    .skip(1)
    .debounce(500, TimeUnit.MILLISECONDS)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(getOps().getdata());
like image 178
Dan Lew Avatar answered Dec 22 '25 20:12

Dan Lew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!