title: Waiting for asynchTask to finish
body: <p>I'm doing my first android app. On one of the page, i need to connect to a Drupal site using REST services, retrieve some data (a list of videos url, their title, a description etc) and display it in a list. When i click on the list, i go to the details of the video.</p>
  
  <p>Here is how i want to proceed. 
  1° Fetching all the data from the drupal site.
  2° When clicking on an item of the list, passing the detail of that video to the next activity.</p>
  
  <p>The problem is this:
  When connecting to the internet in android 3+, you can't do it on the main thread so i had to use AsyncTask to get the data. This worked, but then I want to save the videos in an ArrayList, and then access that list using either a getVideos() or getVideo(index) function. The first to populate the list, the second to retrieve data before going to the details activity. The problem is that the list is not yet populated when i try to access the list of videos.</p>
  
  <p>Technically i don't really need/want to use the asynctask, but connecting to internet on the main thread throws an error saying it's not the proper way to do things.</p>
  
  <p>Here is a simplified version on how i get the videos:</p>
  
  <pre><code>    public class VideoDaoImpl {
          private List&lt;Video&gt; videos ;
  
          public VideoDaoImpl (){
              videos = new ArrayList&lt;Video&gt;();
              new VideoTask(...).execute(); //fetch the videos in json format and 
              //call function onRemoteVideoCallComplete using a handler 
              //in the onPostExecute() function of the asyncTask
          }
  
          public List&lt;Video&gt; getVideos() {
              return videos;
          }
  
          public Video getVideo(int index){
              return videos.get(index);
          }
  
          public onRemoteVideoCallComplete(JSONObject json) {
              //transform Json into videos and add them to the videos arraylist
          }
  
      }
  </code></pre>
  
  <p>This is how i want to fill the video list in my activity:</p>
  
  <pre><code>    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_videos_list);
          //get all videos (not yet filled since faster than second thread)
          List&lt;Video&gt; videos = videoDao.getVideos();
          //get a list of all the titles to display as the list
          List&lt;String&gt; formattedVideos = VideoHelper.formatVideosList(videos);
  
          ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this,
                  android.R.layout.simple_list_item_1, formattedVideos);
  
          ListView listView = (ListView) findViewById(R.id.videos_list);
          listView.setAdapter(adapter);
          listView.setOnItemClickListener(new VideosListItemClickListener(this));
      }
  </code></pre>
  
  <p>So the question really is this. Is there a better way to to this, or is there a way to wait for the list to be filled. </p>