title: BaseAdapter.getView() for ListActivty using ViewHolder - Android
body: <p>This was my original getView method for the ListView BaseAdapter:</p>
  
  <pre><code>    // create a new ImageView for each item referenced by the Adapter
          public View getView(int position, View convertView, ViewGroup parent) {
              ImageView imageView;
              if (convertView == null) {  // if it's not recycled, initialize some attributes
                  imageView = new ImageView(mContext);
                  imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
                  imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                  imageView.setPadding(8, 8, 8, 8);
              } else {
                  imageView = (ImageView) convertView;
              }
  
              ImageHelper ih = new ImageHelper();
  
  
  
  imageView.setImageBitmap(ih.decodeSampledBitmapFromResource(mContext.getResources(), mList.get(position), 40, 40));
  
              return imageView;
          }
  </code></pre>
  
  <p>It did not make use of convertView and I had no mInflater. I read that this way is not good for memory usage, and I found a better way that uses a ViewHolder. I have changed this method to implement a viewHolder and use the convertView instead making an ImageView like I originally did:</p>
  
  <pre><code>static class ViewHolder{
          TextView text;
          ImageView icon;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                ImageView imageView = new ImageView(mContext);
                ImageHelper ih = new ImageHelper();
                if (convertView == null) {
                convertView = mInflater.inflate(R.id.gridviewViewOutfits, null);
  
                holder = new ViewHolder();
                holder.icon = (ImageView) convertView;
                holder.icon.setLayoutParams(new GridView.LayoutParams(150, 150));
                holder.icon.setScaleType(ImageView.ScaleType.FIT_CENTER);
                holder.icon.setPadding(8, 8, 8, 8);
  
                convertView.setTag(holder);
                } else {
                holder = (ViewHolder) convertView.getTag();
                }
  
                holder.icon.setImageBitmap(ih.decodeSampledBitmapFromResource(mContext.getResources(), mList.get(position), 40, 40));
  
                return convertView;
                }
  </code></pre>
  
  <p>This line caused a nullPointerException: <code>convertView = mInflater.inflate(R.id.gridviewViewObjects, null);</code></p>
  
  <p>It's because I had not instantiated my mInflater. Then I did that like this: <code>mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);</code> </p>
  
  <p>Because I saw that online. Then I got this error:</p>
  
  <pre><code>01-20 20:37:52.080: E/AndroidRuntime(22543): FATAL EXCEPTION: main
  01-20 20:37:52.080: E/AndroidRuntime(22543): android.content.res.Resources$NotFoundException: Resource ID #0x7f04002b type #0x12 is not valid
  01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2136)
  01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.content.res.Resources.getLayout(Resources.java:865)
  01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
  01-20 20:37:52.080: E/AndroidRuntime(22543):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
  01-20 20:37:52.080: E/AndroidRuntime(22543):    at com.btf271.imagehelper.ChooseStickersImageAdapter.getView(ChooseObjectsImageAdapter.java:136)
  </code></pre>
  
  <p>What should I do to instantiate the mInflater, or am I inflating the wrong thing? </p>
  
  <p>This is my XML for the ListActivity:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context=".ViewOutfitsActivity" &gt;
  
      &lt;ListView
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true" &gt;
      &lt;/ListView&gt;
  
      &lt;GridView
          android:id="@+id/gridviewViewObjects"
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent"
          android:columnWidth="90dp"
          android:numColumns="auto_fit"
          android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp"
          android:stretchMode="columnWidth"
          android:gravity="center"
      /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Thank you. </p>