tags:

views:

82

answers:

2

How can I color code individual rows in a ListView exactly like it is done in the native MESSAGING app? I do NOT want to simply do alternating rows, but I want to copy the format used in the MESSAGING app, where message rows have a different background color based on the username of the message.

FYI: I am currently extending SimpleAdapter and overriding getView(int position, View convertView, ViewGroup parent) . In this method I am trying to calculate setting the background color based on the position as compared to a list of 'positions to highlight' that I am maintaining each time I update the list, but so far this is only working the first time the list is updated.

In class that overrides SimpleAdapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  View view = super.getView(position, convertView, parent);  

  if(highlightPositions.contains(new Integer(position))){
      view.setBackgroundColor(highlightColor);        
  }
  else{
      view.setBackgroundColor(normalColor);      
  }   
  return view;
}

Thank you for any guidance you can offer!

A: 

I think you need to call notifyDataSetChanged() on your ListAdapter. This forces the list to refresh, calling your overridden getView() method.

Jeff Barger
Yes, this is exactly what I am currently doing. The problem is that I am not clear on how the getView() method gets called on each update and it is currently not simply setting the background color of the view at the position in the list that I want, but setting them all to the same color.
@johnrock: Can you step through your getView() method in the debugger and see exactly what is happening? Do they all get set to the highlight color, or do they all get set to the normal color?
Jeff Barger
A: 

Thanks for the help. Actually, the code I listed does in fact work. The problem turned out to be a small mistake in my keeping track of the list positions to highlight in the highlightPositions list. Once I corrected that, and in fact highlightPositions list did indeed contain the correct positions to highlight, then it worked properly.