title: How to dynamically update a ListView with custom adapter?
body: <p>I have a main activity that creates a <code>ListView</code> and a <code>Custom Adapter</code>. I'm able to populate the <code>ListView</code> if I have the List already created beforehand, but how do I populate it with dynamically fetched data?</p>
  
  <p><strong>MainActivity</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>public class MainActivity extends Activity {
    private ListView myListView;
    private Context ctx;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.MainActivity);
      ctx = this;
      List&lt;Items&gt; myList = new ArrayList&lt;Items&gt;();
  
      myList.add(new Item("Name 1", "Desc 1"));
      myList.add(new Item("Name 2", "Desc 2"));
      myList.add(new Item("Name 3", "Desc 3"));
      myList.add(new Item("Name 4", "Desc 4"));
      myList.add(new Item("Name 5", "Desc 5"));
  
      myListView = (ListView)findViewById(R.id.list);
      MyListAdapter myAdapter = new MyListAdapter(ctx,R.layout.listitem, myList);
      myListView.setAdapter(myAdapter);
    }
  }
  </code></pre>
  
  <p><strong>MyListAdapter</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>public class MyListAdapter extends ArrayAdapter&lt;Items&gt; {
  
    private int resource;
    private LayoutInflater mLayoutInflater;
  
    public MyListAdapter ( Context ctx, int resourceId, List&lt;Items&gt; objects) {
  
      super( ctx, resourceId, objects );
      resource = resourceId;
      mLayoutInflater = LayoutInflater.from( ctx );
    }
  
    @Override
    public View getView ( int position, View convertView, ViewGroup parent ) {
  
      convertView = ( RelativeLayout ) mLayoutInflater.inflate( resource, null );
  
      Items item = (Items) getItem( position );
  
      TextView txtName = (TextView) convertView.findViewById(R.id.listName);
      txtName.setText(item.getName());
  
      TextView txtDesc = (TextView) convertView.findViewById(R.id.listDescription);
      txtDesc.setText(item.getDesc());
  
      return convertView;
    }
  }
  </code></pre>
  
  <p><strong>Item</strong></p>
  
  <pre class="lang-js prettyprint-override"><code>public class Item {
  
  private String name;
    private String desc;
  
    public Item(String name, String desc) {
      super();
      this.name = name;
      this.desc = desc;
    }
  
    //getters and setters
  }
  </code></pre>
  
  <p>How can I modify my code such that a function in the background retrieves the items to the custom adapter and populates the ListView? I tried using an <code>AsyncTask</code> but couldn't manage to get it to work.</p>
  
  <hr>
  
  <p><strong>Edit</strong>: Added the following <code>AsyncTask</code> in <code>MainActivity</code> after <code>onCreate()</code> just to test things out. It's working in that I can see the counting from 1 to 5 and then done.<br>
  How do I get the <code>AsyncTask</code> to update my adapter and <code>ListView</code> though?<br>
  What should <code>onCreate()</code> pass to the <code>AsyncTask</code> and what should <code>AsyncTask</code> return?</p>
  
  <pre class="lang-js prettyprint-override"><code>private class GetItems extends AsyncTask&lt;Void, Integer, Void&gt; {
  
    @Override
    protected void onPreExecute() {
      super.onPreExecute();
      TextView myMsg = (TextView)findViewById(R.id.topMsg);
      myMsg.setText("Loading...");
    }
  
    @Override
    protected Void doInBackground(Void... params) {
      TextView myMsg = (TextView)findViewById(R.id.topMsg);
      for (int i=1;i&lt;=5;i++) {
        try {
          Thread.sleep(1000);
          publishProgress(i);
        } catch (InterruptedException e) {
        }
      }
      return null;
    }
  
    protected void onProgressUpdate(Integer... values) {
      TextView myMsg = (TextView)findViewById(R.id.topMsg);
      myMsg.setText(Integer.toString(values[0].intValue()));
    }
  
    @Override
    protected void onPostExecute(Void result) {
      TextView myMsg = (TextView)findViewById(R.id.topMsg);
      myMsg.setText("Done!");
    }
  }
  </code></pre>