title: Put different background colors for each item in a ViewHolder
body: <p>I have created a GridView using a ViewHolder Adapter to include in each item an image and some text.<br>
  The code is correctly working and the display is smooth, but now I want to add a different background colors to each item. I have checked severals posts like <a href="https://stackoverflow.com/questions/33793853/android-viewholder-background-color">this one</a> or <a href="https://stackoverflow.com/questions/30836840/recyclerview-backgroundcolor">this one</a>, but I cannot manage to make it work.</p>
  
  <p>The GridViewAdapter is created in <code>Main Activity</code>:</p>
  
  <pre><code>private List&lt;GridViewItem&gt; mItems;    // GridView items list
  private GridViewAdapter mAdapter;    // GridView adapter
  </code></pre>
  
  <p>And is used in <code>onCreate</code> as follows:</p>
  
  <pre><code>mItems = new ArrayList&lt;GridViewItem&gt;();
  Resources resources = getResources();
  
  mItems.add(new GridViewItem(resources.getDrawable(R.drawable.truck_package), getString(R.string.drop_package)));
  mItems.add(new GridViewItem(resources.getDrawable(R.drawable.man_package), getString(R.string.pick_package)));
  mItems.add(new GridViewItem(resources.getDrawable(R.drawable.register), getString(R.string.register)));
  mAdapter = new GridViewAdapter(this, mItems);
  // initialize the GridView
  final GridView gridView = (GridView) this.findViewById(R.id.gridView);
  gridView.setAdapter(mAdapter);
  </code></pre>
  
  <p>My GridViewAdapter works as follows:</p>
  
  <pre><code>public class GridViewAdapter extends BaseAdapter {
      private Context mContext;
      private List&lt;GridViewItem&gt; mItems;
  
  
      public GridViewAdapter(Context context, List&lt;GridViewItem&gt; items) {
          mContext = context;
          mItems = items;
      }
  
      @Override
      public int getCount() {
          return mItems.size();
      }
  
      @Override
      public Object getItem(int position) {
          return mItems.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder viewHolder;
  
          if(convertView == null) {
              // inflate the GridView item layout
              LayoutInflater inflater = LayoutInflater.from(mContext);
              convertView = inflater.inflate(R.layout.gridview_item, parent, false);
  
              // initialize the view holder
              viewHolder = new ViewHolder();
              viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
              viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
              convertView.setTag(viewHolder);
          } else {
              // recycle the already inflated view
              viewHolder = (ViewHolder) convertView.getTag();
          }
  
          // update the item view
          GridViewItem item = mItems.get(position);
          viewHolder.ivIcon.setImageDrawable(item.icon);
          viewHolder.tvTitle.setText(item.title);
  
          return convertView;
      }
  
      /**
       * The view holder design pattern prevents using findViewById()
       * repeatedly in the getView() method of the adapter.
       *
       * @see ://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
       */
      private static class ViewHolder {
          ImageView ivIcon;
          TextView tvTitle;
      }
  }
  </code></pre>
  
  <p>Finally, each item of the GridView is defined as follows:</p>
  
  <pre><code>public class GridViewItem {
      public final Drawable icon;       // the drawable for the ListView item ImageView
      public final String title;        // the text for the GridView item title
  
  
      public GridViewItem(Drawable icon, String title) {
          this.icon = icon;
          this.title = title;
  
      }
  }
  </code></pre>
  
  <p>My guess is that I should add a "color" variable in the <code>GridViewItem</code>. Then use it with <code>viewHolder.itemView.setBackgroundColor(color)</code> in <code>GridViewAdapter</code> per both the text and the image. Do you think is the right approach? If so, how would you do it?</p>
  
  <p>Finally, and I am not totally sure if this is the most optimal way to use the gridView, so I would appreciate to know if there is a better way to implement it. Thanks!</p>