Gulshan Yadav
Gulshan Yadav

Reputation: 451

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

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.

Upvotes: 2

Views: 1026

Answers (2)

GhostCat
GhostCat

Reputation: 140613

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

Upvotes: 2

isstiaung
isstiaung

Reputation: 611

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);
        .......
    }
}

Upvotes: 5

Related Questions