title: Android: GridView with images, how set dynamic size and improve performance
body: <p>I want to do an image manager that allows me to add or delete some photos.
  I don't wanna use external libraries, if possible. Now I tried to do and adapter for the GridView that shows some pictues, taken from the user using his camera, and I've got some questions:</p>
  
  <ol>
  <li>I wanna show 3 images for each row, how can I do that? (now I set the size with imageView.setLayoutParams(layoutParams);)</li>
  <li>Performance isn't really good, how can I improve it? Because theorically I should put a big number of pictures.</li>
  </ol>
  
  <p>Here is my adapter:</p>
  
  <pre><code>public class CustomGalleryAdapter extends BaseAdapter {
  
  private Context context;
  private List&lt;String&gt; resources;
  
  // Constructor
  public CustomGalleryAdapter(Context context, List&lt;String&gt; resources){
      super();
  
      this.context = context;
      this.resources = resources;
  }
  
  @Override
  public int getCount() {
      return resources.size();
  }
  
  @Override
  public Object getItem(int position) {
      return resources.get(position);
  }
  
  @Override
  public long getItemId(int position) {
      return 0;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      // Crea un ImageView
      ImageView imageView = new ImageView(context);
  
      // Carica un file dal path dato
      File imgFile = new  File(resources.get(position));
  
      // Se l'immagine esiste
      if(imgFile.exists()){
  
          // Decodifica il Bitmap
          Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
  
          // Setta il bitmap
          imageView.setImageBitmap(myBitmap);
      }
  
      // Setta le proprietà dell'immagine
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
      GridView.LayoutParams layoutParams = new GridView.LayoutParams(100, 150);
      imageView.setLayoutParams(layoutParams);
  
      // Ritorna l'immagine
      return imageView;
  } 
  }
  </code></pre>
  
  <p>Here is my layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;GridView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/gridView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_margin="20dp"
      android:numColumns="auto_fit"
      android:columnWidth="90dp"
      android:horizontalSpacing="10dp"
      android:verticalSpacing="10dp"
      android:gravity="center"
      android:stretchMode="columnWidth" &gt; 
  &lt;/GridView&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong><br/>
  I tried to modify, added convertView check and ViewHolder, but I've no idea how to add LruCache support. Help is very much appreciated!<br/></p>
  
  <p>getView method:</p>
  
  <pre><code>@Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      ViewHolder holder;
      if (convertView == null) {
  
          Log.d("IMAGES","convertView = null");
          mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          holder = new ViewHolder();
          convertView = mInflater.inflate(R.layout.cell_image, null);      
          holder.imageView = (ImageView) convertView.findViewById(R.id.cellImage);   
          convertView.setTag(holder);
      } else {
          holder = (ViewHolder) convertView.getTag();
      }           
  
      Log.d("IMAGES","decoding...");
  
      // Decodifica il Bitmap
      Bitmap myBitmap = BitmapFactory.decodeFile(resources.get(position));
  
      // Setta il bitmap
      holder.imageView.setImageBitmap(myBitmap);
  
      return convertView;
  } 
  </code></pre>
  
  <p>ViewHolder:</p>
  
  <pre><code>class ViewHolder {
      ImageView imageView;
  }
  </code></pre>
  
  <p>cell_image:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ImageView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/cellImage"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scaleType="centerCrop"
      android:adjustViewBounds="true" &gt;
  
  &lt;/ImageView&gt;
  </code></pre>