title: How to change a list item rendering according to its content?
body: <p>I am using a <code>ListFragment</code> with a <code>CursorAdapter</code> to display a list of items. The XML layout is pretty simple: it only consists of the title, a description and the number of comments.</p>
  
  <p><img src="https://i.stack.imgur.com/5oTPP.png" alt="Simple list item layout"></p>
  
  <p>Instead, I would like to show the <strong>number of comments on the right side</strong>. If it is possible I would like to add an image or a colored box as a <strong>background frame</strong>. Further, I would like to <strong>change the image/color according to the number of comments</strong>.</p>
  
  <p><img src="https://i.stack.imgur.com/DGBAZ.png" alt="List item layout"></p>
  
  <p>This is the layout file I am currently using.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent" android:layout_height="fill_parent"
      android:orientation="vertical" android:padding="@dimen/padding_small" &gt;
  
      &lt;TextView
          android:id="@+id/title"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
      &lt;/TextView&gt;
  
      &lt;TextView
          android:id="@+id/description"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
      &lt;/TextView&gt;
  
      &lt;TextView
          android:id="@+id/comments_count"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content" &gt;
      &lt;/TextView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Here the CursorAdapter that I use ...</p>
  
  <pre><code>public class CustomCursorAdapter extends CursorAdapter {
  
      private LayoutInflater mInflater;
  
      public CustomCursorAdapter(Context context, Cursor cursor, int flags) {
          super(context, cursor, flags);
          mInflater = LayoutInflater.from(context);
      }
  
      public void bindView(View view, Context context, Cursor cursor) {
          ViewHolder holder = (ViewHolder)view.getTag();
          if (holder == null) {
              holder = new ViewHolder();
              holder.title = (TextView)view.findViewById(R.id.title);
              holder.comments_count = (TextView)view.findViewById(R.id.comments_count);
              view.setTag(holder);
          }
          String title = cursor.getString(cursor.getColumnIndex(TITLE));
          holder.title.setText(title);
          int comments_count = cursor.getInt(cursor.getColumnIndex(COMMENTS_COUNT));
          holder.comments_count.setText(comments_count + "");
      }
  
      @Override
      public View newView(Context context, Cursor cursor, ViewGroup parent) {
          return mInflater.inflate(R.layout.list_item, parent, false);
      }
  
      private static class ViewHolder {
          TextView title;
          TextView comments_count;
      }
  
  }
  </code></pre>
  
  <p>Here is a <strong>color function</strong> I prepared according to Thiago Moreira Rocha's example implementation ...</p>
  
  <pre><code>if (comments_count == 0) {
      holder.comments_count.getParent().setBackgroundColor(Color.WHITE);
  }
  else if (comments_count != 0) {
      float saturation = (comments_count * 15) / 100.f;
      // The value gets pinned if out of range.
      int color = Color.HSVToColor(new float[] {110f , saturation, 1f});
      holder.comments_count.getParent().setBackgroundColor(color);
  }
  </code></pre>
  
  <p>How would you implement the layout and the context sensitive behavior?</p>
  
  <hr>
  
  <p><strong>Note:</strong></p>
  
  <p>I created a second question to discuss the option to <a href="https://stackoverflow.com/questions/12431592/android-how-to-attach-multiple-touch-actions-to-a-single-list-item">make the comment box clickable</a>. Please refer to the new post if you want to add information to this topic.</p>