title: Populate list outside of async task
body: <p>I'm trying to populate a listview with json data (from the web) through an aSync task.  I have created a list called myCars earlier on in the script and am having trouble populating it from inside the async thread. </p>
  
  <pre><code>package com.*****.complexlistview;
  
  import android.app.Activity;
  import android.content.Context;
  import android.net.ConnectivityManager;
  import android.net.NetworkInfo;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageView;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import org.json.JSONArray;
  import org.json.JSONObject;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Reader;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class MainActivity extends Activity {
      private List&lt;Car&gt; myCars = new ArrayList&lt;Car&gt;();
      protected String[] mBlogPostTitles;
      public static final int NUMBER_OF_POSTS = 20; //caps indicate constants
      public static final String TAG = MainActivity.class.getSimpleName();//prints name of class without package name
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if(isNetworkAvailable()) {
              GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask(); // new thread
              getBlogPostsTask.execute();// don't call do in background directly
              populateListView();
          }else{
              Toast.makeText(this, "Network is unavailable", Toast.LENGTH_LONG).show();
          }
      }
      public boolean isNetworkAvailable() {
          ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = manager.getActiveNetworkInfo();
  
          boolean isAvailable = false;
  
          if(networkInfo != null &amp;&amp; networkInfo.isConnected()){
              isAvailable = true;
          }
  
          return isAvailable;
      }
      private class GetBlogPostsTask extends AsyncTask&lt;Object, Void, String&gt; {
  
          @Override
          protected String doInBackground(Object[] params) {
  
              int responseCode = -1;//need to have this variable outside scope of try/catch block
  
              try {
                  URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
                  HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
                  connection.connect();
  
                  responseCode = connection.getResponseCode();
                  if(responseCode == HttpURLConnection.HTTP_OK){ //could have used just 200 value
                      InputStream inputStream = connection.getInputStream();
                      Reader reader = new InputStreamReader(inputStream);
                      int contentLength = connection.getContentLength();
                      char[] charArray = new char[contentLength];
                      reader.read(charArray);
                      String responseData = new String(charArray);
  
                      JSONObject jsonResponse = new JSONObject(responseData);
                      String status = jsonResponse.getString("status");
                      Log.v(TAG, status);
  
                      JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
                      for(int i=0; i &lt; jsonPosts.length(); i++ ){
                          JSONObject jsonPost = jsonPosts.getJSONObject(i);
                          String title = jsonPost.getString("title");
                          Log.v(TAG, "Post " + i + ": " + title);
                          myCars.add(new Car(title, 1994, R.drawable.kanye8080s, "Lovable"));
  
                          /*myCars.add(new Car("Ford", 1940, R.drawable.stadiumarcadium, "Needing work"));
                          myCars.add(new Car("Toyota", 1994, R.drawable.kanye8080s, "Lovable"));
                          myCars.add(new Car("Honda", 1999, R.drawable.meteora, "Great condition"));
                          myCars.add(new Car("Porsche", 2005, R.drawable.olp, "Awesome"));
                          myCars.add(new Car("Jeep", 2010, R.drawable.yeezus, "Out of this world"));
                          myCars.add(new Car("Honda", 1999, R.drawable.meteora, "Great condition"));
                          myCars.add(new Car("Porsche", 2005, R.drawable.olp, "Awesome"));
                          myCars.add(new Car("Jeep", 2010, R.drawable.yeezus, "Out of this world"));*/
                      }
                  }else{
                      Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
                  }
              }
              catch (MalformedURLException e){
                  Log.e(TAG, "Exception caught");
              }
              catch (IOException e){
                  Log.e(TAG, "Exception caught");
              }
              catch (Exception e){//must be in this order, this is the last, general catch
                  Log.e(TAG, "Exception caught");
              }
  
              return "Code: " + responseCode;
          }
      }
      private void populateListView() {
          ArrayAdapter&lt;Car&gt; adapter = new MyListAdapter();
          ListView list = (ListView) findViewById(R.id.carsListView);
          list.setAdapter(adapter);
      }
  
      private class MyListAdapter extends ArrayAdapter&lt;Car&gt;{
          public MyListAdapter() {
              super(MainActivity.this, R.layout.item_view, myCars);
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              // make sure we have a view to work with
              View itemView = convertView;
              if (itemView == null) {
                  itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
              }
  
              //find the car to work with
              Car currentCar = myCars.get(position);
  
             //fill the view
              ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
              imageView.setImageResource(currentCar.getIconID());
  
              //Make:
              TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
              makeText.setText(currentCar.getMake());
  
              //Year
              TextView yearText = (TextView) itemView.findViewById(R.id.item_txtYear);
              yearText.setText("" + currentCar.getYear());
  
              //Condition
              TextView conditionText = (TextView) itemView.findViewById(R.id.item_txtCondition);
              conditionText.setText(currentCar.getCondition());
  
  
              return itemView;
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  } 
  </code></pre>
  
  <p>The myCars list is supposed to get populated inside of the doInBackground function.  After, inside of the main activity function, it should call the populateListView function which ties it all together.  I'm having difficulty though getting the data out.  </p>
  
  <p>Any help would be greatly appreciated, thanks,</p>
  
  <p>-- 24x7 </p>