title: How to Populate a GridView with Drawables One Cell at a time
body: <p>I was wondering if anyone had any idea of how to go about setting up an adapter to populate a GridView One cell at a time. I've managed to create a custom adapter that will populate every cell, but I want to do it one cell at a time based upon a click. In other words, you click a button, a image populates the first cell in the grid view. You press the button again, a image populates the second cell in the Grid View and So on. My Adapter code is shown below.</p>
  
  <pre><code>public class ImageAdapter extends BaseAdapter {
  
      private Context myContext;
  
  
      public ImageAdapter(Context c) {
          myContext = c;
      }
  
      public int getCount() {
          return imageId;
      }
  
      public Object getItem(int position) {
          return null;
      }
  
      public long getItemId(int position) {
          return 0;
      }
  
      //Create a new ImageView for each Item referenced by the adapter
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ImageView image;
          if (convertView == null) {
              //If Image is not Recycled, set these attributes
              image = new ImageView(myContext);
              image.setLayoutParams(new GridView.LayoutParams(85,85));
              image.setScaleType(ImageView.ScaleType.CENTER_CROP);
              image.setPadding(4,4,4,4);
          } else {
              image = (ImageView) convertView;
          }
          image.setImageResource(imageId);
          return image;
      }
  
  
      //Reference to The Images
      private Integer imageId =
              R.drawable.red_ball;
  
  }
  </code></pre>