title: Android getView and ImageAdapter
body: <p>I would like to implement that kind of <a href="http://vikaskanani.wordpress.com/2011/07/20/android-custom-image-gallery-with-checkbox-in-grid-to-select-multiple/" rel="nofollow">gallery</a>, but with the difference of getting images remotly.
  The idea is to update the view only when an image has been downloaded. 
  => I need to update the view only when going to onComplete.</p>
  
  <p>Please find below my code :</p>
  
  <pre><code>public class displayAlbums extends Activity {
  
  
      private static String URL_GRAPH_API = "https://graph.facebook.com/";
      private static final String TAG = "displayAlbums";
      private ArrayList&lt;Bitmap&gt; thumbnails = new ArrayList&lt;Bitmap&gt;();
      private ArrayList&lt;URI&gt; thumbnails_uri = new ArrayList&lt;URI&gt;();
      private ImageAdapter imageAdapter;
      private int imageCounter = 0;
      public boolean[] thumbnailsselection;
      public Albums album;
      private Context displayAlbums_context;
  
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.galleryitem);
          this.displayAlbums_context = this;
          Log.d(TAG, "on create");    
          findViewById(R.id.itemCheckBox).setVisibility(View.GONE);
          final GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);        
          populateThumbnailsList();
  
  
  
  
          processThumbnailsToDownload(thumbnails_uri, new PictureRequestListener() {
  
              @Override
              public void onComplete(Bitmap bitmap) {
  
                  if(imageAdapter == null) {
                      Log.d(TAG, "image adapter is null");
                      imageAdapter = new ImageAdapter(displayAlbums_context);
                      imagegrid.setAdapter(imageAdapter); 
                  }
                  imagegrid.setId(imageCounter);
                  thumbnails.add(bitmap);     
                  imageCounter++;
                  //imageAdapter.notifyDataSetChanged();
              }
          }); 
  
      }
  
  
  
  
      public void processThumbnailsToDownload(ArrayList&lt;URI&gt; thumbnails_uri, final FbRequestListener listener)  {     
          Log.v(TAG,"processThumbnailsToDownload CALLED");
          for(URI thumbnail_uri : thumbnails_uri ) { 
              new HttpConnection(ConnectionManager.getConnectionHandler(listener)).bitmap(thumbnail_uri);
          }
      }
  
  
  
      public void populateThumbnailsList() {
          URI uri = null;
  
          for(final Albums album : LoadObjects.albums  ) {    
              Bundle parameters = new Bundle();
              parameters.putString("access_token", FbpicturesActivity.mFacebook.getAccessToken());            
              try {
                  uri = new URI(URL_GRAPH_API + album.getAid() + "/picture" + "?" +Util.encodeUrl(parameters) );
                  thumbnails_uri.add(uri);
              } catch (URISyntaxException e) {
                  e.printStackTrace();
              }   
          }
      }
  
  
  
  public class ImageAdapter extends BaseAdapter {
  
  
          private LayoutInflater mInflater;
          private Context myContext;
  
          public ImageAdapter(Context displayAlbums_context) {
              Log.v(TAG,"IMAGE ADAPTER");
              mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  
          }
  
          public int getCount() {
              Log.d(TAG,"thumnail_uri size"+thumbnails_uri.size());
              return thumbnails_uri.size();
          }
  
          public Object getItem(int position) {
              return position;
          }
  
          public long getItemId(int position) {
              return position;
          }
  
          public View getView(final int position, View convertView, ViewGroup parent) {
              Log.v(TAG,"GET VIEW CALLED");
              ViewHolder holder;
              if (convertView == null) {
                  holder = new ViewHolder();
                  convertView = mInflater.inflate(R.layout.galleryitem, null);
                  holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                  holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);               
                  convertView.setTag(holder);
              }
              else {
                  holder = (ViewHolder) convertView.getTag();
              }
  
  
  
  
  
              //holder.checkbox.setId(position);
              //holder.imageview.setId(position);
              Log.d(TAG,"thumnail size"+thumbnails.size());
              Log.d(TAG,"thumnail position"+thumbnails.get(position));
  
              holder.imageview.setImageBitmap(thumbnails.get(position));
              holder.checkbox.setChecked(true);
              holder.id = position;   
  
              return convertView; 
  
  
          }
      }
  
  }   
  
  
  class ViewHolder {
      ImageView imageview;
      CheckBox checkbox;
      int id;
  }
  </code></pre>
  
  <p>galleryItem.xml :</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="fill_parent"&gt;
      &lt;Button android:id="@+id/selectBtn"
          android:layout_width="wrap_content" android:layout_height="wrap_content"
          android:text="Select" android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:minWidth="200px" /&gt;
  
      &lt;GridView android:id="@+id/PhoneImageGrid"
          android:layout_width="fill_parent" android:layout_height="fill_parent"
          android:numColumns="auto_fit" android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp" android:columnWidth="90dp"
          android:stretchMode="columnWidth" android:gravity="center"
          android:layout_above="@id/selectBtn" /&gt;
  
      &lt;ImageView android:id="@+id/thumbImage" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:layout_centerInParent="true" /&gt;
  
      &lt;CheckBox android:id="@+id/itemCheckBox" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:layout_alignParentRight="true"
          android:layout_alignParentTop="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Please note i have added some Log.v to understand when getView is called :</p>
  
  <p>=> <a href="http://pastebin.com/L9PYBYDa" rel="nofollow">http://pastebin.com/L9PYBYDa</a></p>
  
  <p><strong>questions :</strong></p>
  
  <p>1) Why getView is called 4 times ?</p>
  
  <p>2) Why holder.imageview.setImageBitmap(thumbnails.get(position)) returns java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 ?</p>
  
  <p>3) Do you have any advice to implement my gallery with remote image ?</p>
  
  <p>Thank you very much for your help</p>