ListView - paint individual list item in different color
<p>I have a listview and i want to paint each listitem in different color (if it satisfies a condition). I'm calling the setBackgroundDrawable and passing it a GardientDrawable, in the getView of my adapter class. By doing this i get the following exception messages. 
Any idea on how to resolve this is well appreciated..
thanks</p>

<pre><code>05-11 17:54:33.096: ERROR/AndroidRuntime(225): Uncaught handler: thread main exiting due to uncaught exception
05-11 17:54:33.136: ERROR/AndroidRuntime(225): java.lang.NullPointerException
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.onDrawScrollBars(View.java:5589)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6541)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.widget.AbsListView.dispatchDraw(AbsListView.java:1324)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.widget.ListView.dispatchDraw(ListView.java:2972)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6641)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.widget.AbsListView.draw(AbsListView.java:2171)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6538)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6538)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6641)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1529)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6538)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6538)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.drawChild(ViewGroup.java:1531)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1258)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.View.draw(View.java:6538)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1830)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewRoot.draw(ViewRoot.java:1349)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1114)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1633)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.os.Looper.loop(Looper.java:123)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at java.lang.reflect.Method.invoke(Method.java:521)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-11 17:54:33.136: ERROR/AndroidRuntime(225):     at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<hr>

<p>Edited:</p>

<p>here is my getView() code</p>

<pre><code>@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder = null;
  ImageView add_delete_imgView;

  RowData rowData= getItem(position);
  if (null == convertView) {

    convertView = mInflater.inflate(R.layout.list_view_section, null);
    convertView.setScrollbarFadingEnabled(false);
    holder = new ViewHolder(convertView);
    convertView.setTag(holder);
  }

  holder = (ViewHolder) convertView.getTag();

  add_delete_imgView = holder.getAddDeleteImage();
  add_delete_imgView.setImageResource(R.drawable.delete_icon_16);

  return convertView;
}
</code></pre>

<p>On close inspection, I found that if i do a view.findViewById(R.id.add_delete_img) on the view argument of OnItemClick(), it returns null. Below is how i try to access it.</p>

<p>private OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView a, View v, int position, long id) {
ImageView img = (ImageView)v.findViewById(R.id.add_delete_img);
}
}</p>

<p>any help is appreciated...thanks..</p>
