В вашем коде есть ошибки, которые могут вызвать IndexOutOfBoundsException
.
Я добавлю несколько комментариев в ваш код:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init api
Retrofit retrofit = RetrofitClient.getInstance();
btnOk = findViewById(R.id.btn_ok);
myAPI = retrofit.create(IMyAPI.class);
textView = findViewById(R.id.show);
fetchUserData();
// this line should be removed cause you didn't get yet the result of fetchUserData(). Instead you should do this on the onNext method.
// textView.setText(list.get(9));
btnOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Before accessing the list, you must ensure your list contains at least 9 elements. Otherwise it could crash.
// textView.setText(list.get(9));
// This would be the correct way
if (list != null && list.size > 9) {
textView.setText(list.get(9));
}
}
});
}
private void fetchUserData() {
myAPI.getUsers()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<ArrayList<User>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ArrayList<User> users) {
String usName ;
for(int i = 0; i < users.size(); i++){
usName = posts.get(i).getUsername();
list.add(usName);
}
// This is the place where you are safe to access the users list, so any View component you want to update with the users list should be done here.
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
Помните, что вы используете Rx для вызова asynchronous
, вам следует подождать, пока метод onNext
не будетвызывается перед выполнением любых операций с результатом.