title: Android app shows listview only after running for the second time
body: <p>My app is supposed to get some info from a website (JSON format), parse it, store it in a SQLite database, then show a list of items (obtained from the database) in a list view. It does everything (checked with Log.v messages) except populate the listview. But if I run it for the second time the listview is displayed.</p>
  
  <p>In MainActivity I use an AsyncTask to get the data after it is parsed. Then I start another activity to read data from a table (all rows) and display them in the ListView.</p>
  
  <pre><code>public class MainActivity extends Activity {
  private MySQLiteHelper dbHelper; 
  private SQLiteDatabase db;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      String url = "http://strong-earth-32.heroku.com/stores.aspx";
  
      dbHelper = new MySQLiteHelper(this); 
      db = dbHelper.getWritableDatabase();
  
      ConnectAsyncTask connect = new ConnectAsyncTask(url);
      connect.execute();
      try {
          connect.wait(2000);
      } catch (Exception e) {
          Log.v("Waiting. ",e.getStackTrace().toString());
      }
      Intent intent=new Intent(MainActivity.this,DisplayActivity.class);
      startActivity(intent);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  public void cacheData(String result) {
      ContentValues cv = new ContentValues();
      try {
          //Tranform the string into a json object
         final JSONObject json = new JSONObject(result);
         JSONArray jArray = json.getJSONArray("stores");
          for(int i = 0; i &lt; jArray.length(); i++) {
              try{
                  JSONObject oneObject = jArray.getJSONObject(i);
                  String logoURL = oneObject.getString("storeLogoURL");
                  String address = oneObject.getString("address");
                  cv.put(MySQLiteHelper.COLUMN_ADDRESS, address);
                  String phoneNr = oneObject.getString("phone");
                  cv.put(MySQLiteHelper.COLUMN_PHONE, phoneNr);
                  long row = db.insert(MySQLiteHelper.TABLE_STORES, MySQLiteHelper.COLUMN_ID, cv);
                  //Log.v("row= ", row + "");
                  cv.clear();
              } catch (JSONException e) {
                  Log.v("Oops! ",e.getStackTrace().toString());
              }
          }
      } catch (JSONException e) {
          Log.v("ERR",e.getStackTrace().toString());
      }
  }   
  
  
  
  private class ConnectAsyncTask extends AsyncTask&lt;Void, Void, String&gt;{
          private ProgressDialog progressDialog;
          String url;
          ConnectAsyncTask(String urlPass){
              url = urlPass;
          }
          @Override
          protected void onPreExecute() {
              // TODO Auto-generated method stub
              super.onPreExecute();
              progressDialog = new ProgressDialog(MainActivity.this);
              progressDialog.setMessage("Fetching data, Please wait...");
              progressDialog.setIndeterminate(true);
              progressDialog.show();
          }
          @Override
          protected String doInBackground(Void... params) {
              JSONparser jParser = new JSONparser();
              String json = jParser.getJSONFromUrl(url);
              return json;
          }
          @Override
          protected void onPostExecute(String result) {
              super.onPostExecute(result);   
              progressDialog.hide();
              progressDialog.dismiss();
              if (result!=null) {
                  Log.v("READY","READY");
                  cacheData(result);
              }
          }
      }
  
  }
  </code></pre>
  
  <p>Here's the other activity:</p>
  
  <pre><code>public class DisplayActivity extends Activity {
  private MySQLiteHelper dbHelper; 
  private SQLiteDatabase db;
  private ListView lv_custom;
  ArrayList&lt;Content&gt; contents = new ArrayList&lt;Content&gt;();
  Content content0 = new Content();
  Cursor c;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_display);
      lv_custom = (ListView) findViewById(android.R.id.list);
  
      dbHelper = new MySQLiteHelper(this); 
      db = dbHelper.getWritableDatabase();
      readData(db);
      lv_custom.setAdapter(new CustomListviewAdapter(this, R.layout.one_row, contents));
  
  }
  
  private class Content {
      private String logoURL;
      private String phoneNr;
      private String address;
  }
  
  private class ViewHolder{
      WebView webView;
      TextView textViewPhone;
      TextView textViewAddress;
  }
  
  
  private class CustomListviewAdapter extends BaseAdapter{
  
      private int layoutResource;
      private ArrayList&lt;Content&gt; mArrayList;
      private LayoutInflater inflater;
  
  
      public CustomListviewAdapter(Context mContext, int layoutResource, ArrayList&lt;Content&gt; mArrayList) {
          this.layoutResource = layoutResource;
          this.mArrayList = mArrayList;
          inflater = LayoutInflater.from(mContext);
      }
  
      @Override
      public int getCount() {
          // TODO Auto-generated method stub
          return mArrayList.size();
      }
  
      @Override
      public Object getItem(int position) {
          // TODO Auto-generated method stub
          return mArrayList.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          // TODO Auto-generated method stub
          return position;
      }
  
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder viewHolder;
  
              convertView = inflater.inflate(layoutResource, null);
              viewHolder = new ViewHolder();
  
              //WebView web = (WebView) findViewById(R.id.webView1);
              //web.loadUrl("Your Url");
              //viewHolder.webView = (WebView) findViewById(R.id.webView1);
              viewHolder.textViewPhone = (TextView) convertView.findViewById(R.id.phoneNr);
              viewHolder.textViewAddress = (TextView) convertView.findViewById(R.id.address);
  
          //viewHolder.webView.loadUrl(mArrayList.get(position).logoURL);
          viewHolder.textViewPhone.setText(mArrayList.get(position).phoneNr);
          viewHolder.textViewAddress.setText(mArrayList.get(position).address);
  
          //int itemHeight = height/4 - 10;
  
          //viewHolder.webView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, itemHeight));
  
          return convertView;
      }
  
  }
  
  public void readData(SQLiteDatabase db) {
  String[] selectionArguments = {};
  c = db.rawQuery("SELECT * FROM storesList", selectionArguments);
  if (c.getCount() != 0) {
  try {
      while (c.moveToNext()) {
          //add a new store information to the ArrayList
          content0 = new Content();
          //content0.logoURL = logoURL;
          content0.phoneNr = c.getString(2);
          content0.address = c.getString(1);
          contents.add(content0);
          Log.v("address:", content0.address);
      } 
  } catch (Exception e) {
      Log.v("Exception e ",e.fillInStackTrace().toString());
  }}
  else Log.v("Cursor ", 0+"");
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Why don't I see the list from the first execution of the app? I guess it has something to do with the second activity starting before the AsyncTask is done. However, I don't have a solution to fix the problem. Besides, I don't see the ProgressDialog. Any idea? </p>