title: Fill an ArrayList with object from response.body
body: <p>I'm using retrofit to get list of data from api. I want to save the data which I get in body.response to arrayList. I can do that and I get the items from new arrayList, but it seems that the new list are not saved. When I try to use it outside of method onResponse, I get an error </p>
  
  <blockquote>
    <p>Unable to start activity... IndexOutOfBoundsException: Index 0, Size 0</p>
  </blockquote>
  
  <p>The interface</p>
  
  <pre><code> public interface ServerApi {
          @Headers("Accept: application/json")
          @GET("cities")
          Call&lt;ArrayList&lt;City&gt;&gt; getCities();
      }
  </code></pre>
  
  <p>The activity</p>
  
  <pre><code>public class SomeActivity extends AppCompatActivity {
  
      Retrofit retrofit = new Retrofit.Builder()
              .baseUrl("http://url")
              .addConverterFactory(GsonConverterFactory.create())
              .build();
  
      ServerApi serverApi = retrofit.create(ServerApi.class);
  
      ArrayList&lt;City&gt; cityList = new ArrayList&lt;City&gt;();
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_parkings);
  
          final TextView tv1 = findViewById(R.id.tv1);
  
          final Call&lt;ArrayList&lt;City&gt;&gt; cities = serverApi.getCities();
  
          cities.enqueue(new Callback&lt;ArrayList&lt;City&gt;&gt;() {
              @Override
              public void onResponse(Call&lt;ArrayList&lt;City&gt;&gt; call, Response&lt;ArrayList&lt;City&gt;&gt; response) {
                  for(int i=0; i&lt;response.body().size(); i++){
                      City ct = new City(response.body().get(i).getId(), response.body().get(i).getName());
                      cityList.add(ct);
                      tv1.setText(cityList.get(i).getName());
                  }
              }
              @Override
              public void onFailure(Call&lt;ArrayList&lt;City&gt;&gt; call, Throwable t) {
                  tv1.setText("failure " + t);
              }
          });
  
        TextView c1 = findViewById(R.id.c1);
        c1.setText(cityList.get(0).getName());
      }
  }
  </code></pre>
  
  <p>The string <code>tv1.setText(cityList.get(i).getName());</code> inside the method onResponse works well. But when I write it out of the method, the aplication fall </p>