2015-10-29 117 views
2

我是Reactive的新手,所以對我來說很簡單,但我試圖用自動提示功能替換當前在文本更改上運行的異步任務。下面是我在我的RX其中:需要RXJava幫助的AsyncTask替換

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的異步任務有關,因爲這是我需要做的幾個中的第一個。

編輯:

這是我得到的感覺好像我很接近,但我仍然需要guidance.etSearch是我的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); 
      } 
     });} 

回答

2

這是我做到了。謝謝你沒關係StackOverflow haha​​:

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