无法在 call.enqueue 中的 for 循环之外获取 ArrayList [英] can't get ArrayList outside of for loop in call.enqueue

查看:72
本文介绍了无法在 call.enqueue 中的 for 循环之外获取 ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 Retrofit 中的 call.enqueue 方法的 for 循环中获取 ArrayList 数据.

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

如何访问 call.enqueue mehtod 之外的列表?

how to access lists outside of call.enqueue mehtod?

一切正常.打印列表大小时,我得到了我想要的价值.唯一的问题是我无法从 call.enqueue 方法之外访问值.

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

        }
    });

}

我想在 call.enqueue 方法之外访问 ArrayList.

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

推荐答案

最简单的方法可能是声明一个函数,

The simplest way might be to declare a function,

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

并在call.enqueue

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

这篇关于无法在 call.enqueue 中的 for 循环之外获取 ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆