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(highl开发者_开发百科ightColor);
}
else{
view.setBackgroundColor(normalColor);
}
return view;
}
Thank you for any guidance you can offer!
I think you need to call notifyDataSetChanged() on your ListAdapter. This forces the list to refresh, calling your overridden getView() method.
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.
精彩评论