0
我正在使用MVVM與Rxjava,這是我第一次使用Rxjava,我想通過Spinner中的響應填充項目。但我無法這樣做。 spinner如何動態填充?動態使用RX和MVVM模式將項目放入Spinner中?
ApiInterface
@GET("client")
Observable<List<ClientListApi>> clientListApi();
RegisterFragmentVm
public void getClientList() {
ArrayList arrayList = new ArrayList();
ApiInterface apiInterface = setRetrofit();
apiInterface.clientListApi().subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<ClientListApi>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<ClientListApi> clientListApis) {
int position = 0;
System.out.println("size of arraylist" + clientListApis.size());
for (int i = 0; i < position; i++) {
System.out.println("name" + clientListApis.get(position).getName());
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
// ArrayAdapter<String> sector_adapter = new ArrayAdapter<String>(mContext, R.layout.support_simple_spinner_dropdown_item, arrayList);
// sector_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// fragmentRegisterBinding.spinner.setAdapter(sector_adapter);
}
RegisterFragment
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
fragmentRegisterBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_register, container, false);
registerFragmentVm = new RegisterFragmentVm(getContext(), fragmentRegisterBinding);
fragmentRegisterBinding.setRegisterVm(registerFragmentVm);
registerFragmentVm.getClientList();
return fragmentRegisterBinding.getRoot();
}
何這可以解決。我能夠獲得Response,但無法將其填充到Spinner中。