title: In android, how can I separate the data loading into its own thread?
body: <p>So I'm working on my "hello world" application in android/java, and elected to do a sports app (which is strange...I don't like sports...but whatever). So I set up my layout, allow users to 'drill down', so they can see the layout for Baseball, or MLB, or the Indians.  Say a user selects 'Indians' from the MLB view.  I update the tabs, potentially the color scheme, background, etc, and load the data for the 'news' and 'players' tabs (the latter of which is unique to team layouts). Unfortunately, api calls can sometimes take relatively long to complete, especially when the free API from ESPN is capped at 1 call per second. I do some significant caching already, but there's no way I can guarantee that I won't be loading both 'news' and 'players' for 'Indians' at the same time, so one of the requests will have to wait a full second to return. </p>
  
  <p>So my solution is to have a data loading thread - the UI says 'get me this data', and does the UI work not contingent on the data being there. The question though is - once the data is returned from the data loader (as each piece comes back), how should it update or notify the UI appropriately?  My current thought is:</p>
  
  <p>UI thread:</p>
  
  <pre><code>OnSelectIndians()
  {
    DataLoadThread.GetIndiansPlayers();
    DataLoadThread.GetIndiansNews();
    // UI stuff
  }
  
  OnPlayersLoaded(Array Players)
  {
    if (layout == INDIANS_LAYOUT)  // Make sure we haven't changed layouts
    {
      foreach player in Players
        tab[PLAYERS].textview.text += player
    }
  }
  </code></pre>
  
  <p>But this isn't a problem I've had to deal with before. Is this the right way to go about it? Or is there a better/easier design I can use? I don't particularly like requiring the UI thread to have a  'on data returned' method for every type of data I can request. My other loosely-formed idea is to create a lambda function in the UI code, which is passed to the data loader and executed in the data loading thread, so:</p>
  
  <pre><code>DataLoadThread.Queue(
      foreach player in GetIndiansPlayers()
          myView.tab[PLAYERS].textview.text += player;
  );
  </code></pre>
  
  <p>But I think this is probably the worse route, as now we have 2 threads interacting with the UI. Any advice?</p>
  
  <p><strong>Edit:</strong> Okay I got it working using AsyncTask. Out of the box, it still has the problem listed above that I would have to create a new derived class for every type if data I load (so PlayerLoadTask, NewsLoadTask, StandingsLoadTask, etc etc). I also wanted was to have most of the logic visible during the call, so if I'm looking at the event code I know what its doing.  Below is the working implementation - would appreciate any feedback on it, but I'll accept the first answer below just the same.</p>
  
  <p>abstract public class LoadDataHelper {
          public LoadDataHelper(DataLoader dl, Object param) {
              mDataLoader = dl;
              mParam = param;
          }</p>
  
  <pre><code>    abstract public LinkedList&lt;String&gt; LoadData();
      protected DataLoader mDataLoader;
      protected Object mParam;
  }
  
  abstract public class UpdateUIHelper {
      public UpdateUIHelper(MyActivity context) {
          mContext = context;
      }
  
      abstract public void UpdateUI(LinkedList&lt;String&gt; results);
      protected MyActivity mContext;
  }
  
  private class LoadDataTask extends AsyncTask&lt;Void, Void, LinkedList&lt;String&gt; &gt;   {
      private LoadDataHelper mLdh;
      private UpdateUIHelper mUih;
  
      LoadDataTask(LoadDataHelper ldh, UpdateUIHelper uih) {
          mLdh = ldh;
          mUih = uih;
      }
  
      @Override
      protected LinkedList&lt;String&gt; doInBackground(Void... params) {
          return mLdh.LoadData();
      }
  
      @Override
      protected void onPostExecute(LinkedList&lt;String&gt; results) {
          mUih.UpdateUI(results);
      }
  }
  
  //
  // .....
  // 
  
      LoadDataTask task = new LoadDataTask(new LoadDataHelper(mDataLoader, "football") {
                                              public LinkedList&lt;String&gt; LoadData() {                                                  
                                                  return mDataLoader.LoadLeaguesFromSport((String)mParam);
                                              }
                                          }, 
                                          new UpdateUIHelper(this) {
                                              public void UpdateUI(LinkedList&lt;String&gt; results) {
                                                  TextView tv = (TextView)findViewById(R.id.tv1);
                                                  tv.setText("");
                                                  for (String res : results) {
                                                      tv.append(res + "\n");  
                                                  }
                                              }
                                          });
      task.execute();
  </code></pre>