2017-10-20 107 views
2

RxJava修修補補的第一次,我已經創建了一個的Rx鏈聽在String對象的狀態的變化:字符串值如何傳遞給updateText()方法?

observable=Observable 
    .defer(new Callable<ObservableSource<String>>() { 
    @Override 
    public ObservableSource<String> call() throws Exception { 
     return(Observable.just(query())); 
    } 
    }) 
    .subscribeOn(Schedulers.io()) 
    .map(this::prettify) 
    .observeOn(AndroidSchedulers.mainThread()) 
    .cache(); 

這裏美化是:

private String prettify(String raw) { 
Gson gson=new GsonBuilder().setPrettyPrinting().create(); 
JsonElement json=new JsonParser().parse(raw); 

return(gson.toJson(json)); 
} 

現在,我創建了一個訂戶observable

sub=observable.subscribe(
    this::updateText, 
    error -> Toast 
    .makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG) 
    .show() 
); 

但我忍不住明白的是該值是如何傳遞到updateText方法:

private void updateText(String text) { 
((TextView)getView().findViewById(R.id.result)).setText(text); 
} 

的方法updateText簡單的工作,但我的問題是,它是如何得到的字符串值?任何幫助,將不勝感激。

回答

0

這是一個簡單的lambda表達式

sub=observable.subscribe(
    this::updateText, 
    error -> Toast 
    .makeText(getActivity(), error.getMessage(), Toast.LENGTH_LONG) 
    .show() 
); 

this::updateText叫你的方法updateText(String text)

在一種擴展版本,它看起來像 .subscribe(result -> updateText(result), ....

相關問題