can't get ArrayList outside of for loop in call.enqueue

左心房为你撑大大i 提交于 2021-02-10 12:34:26

问题


I want to get ArrayList data out of for loop from call.enqueue method in Retrofit.

how to access lists outside of call.enqueue mehtod?

Everything is working fine. When printing list size I'm getting value what I want. The only problem is i can't access values from outside of call.enqueue method.

private void getSchoolList() {
    final Call<List<DanceSchool>> call = RetrofitClient.getInstance().getApi().getDanceSchools();


    call.enqueue(new Callback<List<DanceSchool>>() {
        @Override
        public void onResponse(Call<List<DanceSchool>> call, Response<List<DanceSchool>> response) {
            if(!response.isSuccessful()) {
                Toast.makeText(ListActivity.this, "Response Code: " + response.code(), Toast.LENGTH_SHORT).show();
            }

            List<DanceSchool> danceSchools = response.body();



            for(DanceSchool danceSchool:danceSchools){

                schoolNameList.add(danceSchool.getSchool_name());
                dayList.add(danceSchool.getDays());
                timingList.add(danceSchool.getSun_timing());
                contactNoList.add(danceSchool.getContact_no());
                addressList.add(danceSchool.getAddress());

            }


        }

        @Override
        public void onFailure(Call<List<DanceSchool>> call, Throwable t) {
            Toast.makeText(ListActivity.this, "Failure: "+t.getMessage(), Toast.LENGTH_SHORT).show();

        }
    });

}

I want to access ArrayList outside of the call.enqueue method.


回答1:


The simplest way might be to declare a function,

void accessArrayList(ArrayList<DanceSchool> danceSchool){
    //do stuff with the values
}

and call it inside call.enqueue

call.enqueue(new Callback<List<DanceSchool>>() {
    @Override
    public void onResponse(....) {
        ........
        List<DanceSchool> danceSchools = response.body();
        accessArrayList(danceSchools);
        .......
    }
}



回答2:


The point is: right now you are making an asynchronous call. You are requesting some information, and when that information has been compiled and is ready then that onResponse() method is invoked. Only then that list can be populated from the incoming response body.

So, what you could do is: have another method in your enclosing class, like updateList(), and then simply call that method from within your onResponse() implementation.

Beyond that, you might want to turn your asynchronous call into a synchronous one. Then, instead, you use execute() to wait for the result to come in (see here for example). But you would still need to have a callback method to call.

Alternatively, you could have a field within the enclosing class, like

List<DanceSchool> danceSchools
...
private void getSchoolList() {
  final Call<List<DanceSchool>> call = ...    
  call.enqueue(new Callback<List<DanceSchool>>() {
    @Override
    public void onResponse(Call<List<DanceSchool>> call, Response<List<DanceSchool>> response) {
    ...
    danceSchools.addAll(response.body());


来源:https://stackoverflow.com/questions/56792215/cant-get-arraylist-outside-of-for-loop-in-call-enqueue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!