title: Android - asynctask that fills an adapter for a listview
body: <p>Basically the flow is like the following: when a fragment is created (or when the user swipes to refresh the layout), an AsyncTask is executed. The ASyncTask retrieves the info from a URL and setup an adapter which is then assigned to a ListView. </p>
  
  <p>This is the code of the Fragment:</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                       Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.tasks_page, container, false);
      mSwipeRefreshLayout = (SwipeRefreshLayout)
      rootView.findViewById(R.id.list_container);
      mSwipeRefreshLayout.setOnRefreshListener(this);
      mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,    
      android.R.color.holo_green_light, android.R.color.holo_red_light);
  
      lv = (ListView) rootView.findViewById(android.R.id.list);
      getTasks();
  
      return rootView;
  }
  
  private void getTasks() {
      new TasksRetriever(mSwipeRefreshLayout,tasksAdapter,lv).execute();
  }
  
  @Override
  public void onRefresh() {
      mSwipeRefreshLayout.setRefreshing(true);
      new Handler().postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      getTasks();
                  }
              }, 1000);
  }
  </code></pre>
  
  <p>The TaskRetriever constructor is:</p>
  
  <pre><code>public TasksRetriever(SwipeRefreshLayout srl, TasksAdapter ta, ListView lv) {
      adpt = ta;
      this.srl = srl;
      this.lv = lv;
  }
  </code></pre>
  
  <p>and postExecute is:</p>
  
  <pre><code>@Override
  protected void onPostExecute(List&lt;TasksItem&gt; result) {
      super.onPostExecute(result);
      dialog.dismiss();
      if (adpt == null) adpt = new TasksAdapter(result);
      else adpt.setItemList(result);
      lv.setAdapter(adpt);
      adpt.notifyDataSetChanged();
      srl.setRefreshing(false);
  }
  </code></pre>
  
  <p>Not sure yet if it works but I was wondering if I'm on the right track because it doesn't look clean to me. On the other hand, I can't create the adapter and assign it to the ListView until I actually have data for it... Is this considering OK or am I doing it wrong?</p>