3 回答
TA贡献1828条经验 获得超4个赞
从服务器获取数据可能会有一些延迟。
因此,您可以添加一个listener将在您的代码打印列表被填充后触发(如果您的打印代码将在另一个类中),或者如果它在同一个类中,则调用另一个方法。
示例代码
List<Profile> profileList = new ArrayList<Profile>();
public void getData(){
final Listener listener = new Listener() {
@Override
public void onDataReceived() {
//print command goes here
}
}
apiService.getGetlistuser("getlistuser").enqueue(new Callback<ResponseDaftar>() {
@Override
public void onResponse(Call<ResponseDaftar> call, Response<ResponseDaftar> response) {
if (response.isSuccessful()) {
List<GetlistuserItem> getlistuserItemList = response.body().getGetlistuser();
for (int i = 0; i < getlistuserItemList.size(); i++) {
profileList.add(new Profile(
getlistuserItemList.get(i).getUserID(),
getlistuserItemList.get(i).getNickname()
));
}
printData(); //if it is in same class
if(listener != null)
listener.onDataReceived(); //if it is going to happen in another class
/*
is working
for (Profile p : profileList){
Log.i("info", p.getUserid() + '\n' + p.getNickname());
}
*/
}
}
});
// my value is empty
for (Profile p : profileList){
Log.i("info", p.getUserid() + '\n' + p.getNickname());
}
}
public void printData(){
for (Profile p : profileList){
Log.i("info", p.getUserid() + '\n' + p.getNickname());
}
}
// this will be going to make in separate file
interface Listener{
void onDataReceived();
}
添加回答
举报