Therefore, I will try to make this question as accurate as possible, but it will include code fragments that cross the whole encoding.
For context, I'm a fairly new and completely self-help tutorial for Android dev, so please let me know of any obvious misunderstandings / poor organization. The main task of the question is the error that I encountered now, namely: after a network request, the variable that should have been set as a result of this network request is zero, because the code moves forward until the network request is completed.
Here is my activity method. It is assumed that it fills the variable with a mFriendsresult mUserPresenter.getUserList()that (unfortunately) null:
@Override
public void onResume(){
super.onResume();
mUserPresenter = new UserPresenter();
mFriends = mUserPresenter.getUserList();
if (mGridView.getAdapter() == null) {
UserAdapter adapter = new UserAdapter(getActivity(), mFriends);
mGridView.setAdapter(adapter);
}
else{
((UserAdapter)mGridView.getAdapter()).refill(mFriends);
}
}
This is how I structure the method UserPresenter getUserList:
public List<User> getUserList()
{
ApiService.get_friends(this);
return mUserList;
}
Real magic happens in the classroom ApiService:
public static void get_friends(final UserPresenter userPresenter){
ApiEndpointInterface apiService = prepareService();
apiService.get_friends().
observeOn(AndroidSchedulers.mainThread())
.subscribe(
new Action1<List<User>>()
{
@Override
public void call(List<User> users) {
userPresenter.setList(users);
}
}
);
}
I thought that by calling userPresenter.setList(users)in ApiService, this would set the mUserListapi to respond from the request. However, instead, mUserList == nullat the time of the answer getUserList.
Any ideas on how I can structure this?
source
share