我編寫了一個由ApiRest填充的autocompleteTextView適配器。我正在使用rxJava和Retrofit 2.但無法獲得過濾結果,因爲我不知道如何返回異步函數中的值。這裏是我的代碼如何在使用RxJava或Retrofit2的AutoCompleteTextView Adapter中使用ApiRest返回FilterResults?
public class DiagnosticoAutoCompleteAdapter extends BaseAdapter implements Filterable {
...
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
final FilterResults filterResults= new FilterResults();
if(charSequence!=null) {
ApiUtils.getAPIServiceDos()
.getDiagnosticos(charSequence.toString())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Diagnostico>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<Diagnostico> value) {
// HERE IT SHOWS ME THE SIZE E.G. 45 so The values are received correctly
System.out.println("tamaño diagnostico::"+value.size());
// HERE IT SHOWS ME THE OBJECT NAME IN POSITION 0 AND ITS OK
System.out.println("contenido diagg...."+value.get(0).getNombre());
filterResults.values=value;
filterResults.count=value.size();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
//here i lost the information. the count is 0
System.out.println("tamaño de filtered results::"+filterResults.count);
return filterResults;
}
RxJava請求工作正常,但不能返回值。
我的問題是如何返回過濾結果?
我通過更改請求並將其置於方法中來解決問題@Override protected void publishResults(CharSequence charSequence – matQ
更新答案,並將解釋請求放在'publishResults' – yosriz