title: Implementing a GalleryAdapter getCount that fetches its content from a remote server
body: <p>This is a looong explanation, but please bear with me... Here be dragons...</p>
  
  <p>Ok so I have an Android Gallery and implemented a GalleryAdapter that creates my views and stuff:</p>
  
  <pre><code>public class GalleryAdapter extends BaseAdapter {....
  ....
  etc
  ...
  </code></pre>
  
  <p>Now, before we start, almost ALL of the examples out there that show you how to do this assume that you know exactly how many images there are going to be in the gallery. In my case, I do not. It could be any arbitrary number of images but at least there's a consistent naming convention like 001, 002, 003 etc  </p>
  
  <p>I have a folder somewhere on a server that has the images in it that should be displayed in the gallery:</p>
  
  <p><a href="http://www.somedomain.com/content/pictures" rel="nofollow">http://www.somedomain.com/content/pictures</a></p>
  
  <p>In that pictures folder we might have<br>
  0001.jpg<br>
  0002.jpg<br>
  0003.jpg<br>
  000n.jpg<br>
  etc ad infinitum</p>
  
  <p>now BaseAdapter which I am extending, expects you to override <code>public int getCount()</code><br>
  So if you know that you have 20 images you simply go:  </p>
  
  <pre><code>public int getCount() {return 20;}  
  </code></pre>
  
  <p>And your on your merry way. But what about my case?<br>
  The way I have gotten around this till now is requiring a count.txt file be placed in the root of that directory where the images are that contains an integer that tells you that there are X amount of images in it. Remember, this is an http location.  </p>
  
  <p>So my getCount looks more like this</p>
  
  <pre><code>public int getCount() {
    int count = getRemoteImageCount();
    if(count == -1)
    {
        return 0;//Oh crap, something went wrong, what now?!
    } else {
      return count;
    }
  
  public int getRemoteImageCount()
    try {
      URL url = new URL(strURL);
      URLConnection conn = url.openConnection();
      conn.setUseCaches(true);
      conn.connect();
  
      InputStream instream = conn.getInputStream();
      InputStreamReader reader = new InputStreamReader(instream);
  
      String strCount = new String();
      int data = reader.read();
      while(data != -1){
        char theChar = (char) data;
        strCount += theChar;
        data = reader.read();
      }
  
      reader.close(); 
      count = Integer.parseInt(strCount);
      } catch (Exception e) {
        count = -1
    } 
  
  
    return count;
  }
  </code></pre>
  
  <p>Ok, now that we have a bit of context, here is the actual question. If by some chance I    fail to fetch the count.txt file and cannot tell how many images there are in the remote folder I need to tell the user that we have failed using a Dialog and ask them if they want to retry or just ignore it. How would I go about doing that?  The next bit of code is my attempt but it fails horribly for a couple of reasons, but mainly because Dialogs are shown synchronously. How do I implement a (Retry/Cancel) to get my GalleryAdapter into a state where eventually the network is back up and I can server those images in the Gallery?</p>
  
  <p>Here's my, non working attempt. Any alternatives suggestions are also welcome.</p>
  
  <pre><code>public int getCount() {
      // TODO fetch image amount remotely     
      int imageCount;
  
      do {
          RemoteBitMapHelper rbmh = new RemoteBitMapHelper();     
          imageCount = rbmh.getBitmapCount(baseUrl + "count.txt");
  
          if(imageCount == -1) {
              imageCount = 0;
              new AlertDialog.Builder(this.context)
              .setTitle("Connection Error")
              .setMessage("There was an error connecting to the server")
              .setPositiveButton("Retry",
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int whichButton) {
                          setRetryGetCount(true);
                      }
                  })
              .setNegativeButton("Continue",
                  new DialogInterface.OnClickListener() {             
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          setRetryGetCount(false);
                      }
                  })
              .setCancelable(false)
              .show();
          }
  
      } while(retryGetCount &amp;&amp; imageCount &lt;= 0);
  
  
      return imageCount;
  }
  </code></pre>