猿问

在改造之外打印时值为空的数组列表

为什么我的数组列表的值为空?当它在改造之外打印时,即使我已经声明了公共变量List<Profile> profileList = new ArrayList<Profile>();


我想要在改造之外打印值数组列表但没有工作。当在改造内打印值数组列表时,它起作用了。


这是我的代码


你好.java


List<Profile> profileList = new ArrayList<Profile>();

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()

                    ));

                }


    /* 

      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());

    }

改造接口


@POST("GetListNews.php")

    @FormUrlEncoded

    Call<ResponseDaftar> getGetlistuser(@Field("TableName") String paramTable);


慕慕森
浏览 109回答 3
3回答

一只甜甜圈

您必须检查响应方法&nbsp;(您评论的地方)中的数据,因为加载响应需要一些时间,有时下一条语句会在响应可用之前运行。

明月笑刀无情

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

慕勒3428872

您正在阅读 profileList 在填充之前。
随时随地看视频慕课网APP

相关分类

Java
我要回答