I'm trying to change the item colors of a listview in android, but I can't make it work. I want the colors to alternate... I've made my own adapter and inside the getView method I changed the color before returning the list view but it doesn't work I don't know why...
Here is my code:
public class EventoAdapter extends ArrayAdapter<Evento>{
Context context;
int layoutResourceId;
ArrayList<Evento> evento = null;
public EventoAdapter(Context context, int textViewResourceId,
ArrayList<Evento> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.layoutResourceId = textViewResourceId;
this.context = context;
this.evento = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
EventoHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new EventoHolder();
holder.nombre = (TextView)row.findViewById(R.id.nombreEvento);
holder.total = (TextView)row.findViewById(R.id.totalEvento);
holder.moneda = (TextView)row.findViewById(R.id.moneda);
holder.fecha = (TextView)row.findViewById(R.id.fecha);
row.setTag(holder);
}else{
holder = (EventoHolder)row.getTag();
}
Evento ev = evento.get(position);
holder.nombre.setText(ev.getNombre());
holder.total.setText(Integer.toString(ev.getTotal()));
holder.moneda.setText("$");
holder.fecha.setText("20/12/11");
if(position%2==0){
row.setBackgroundColor(R.color.colorPar);
}else{
row.setBackgroundColor(R.color.colorImpar);
}
return row;
}
static class EventoHolder{
TextView nombre;
TextView total;
TextView moneda;
TextView fecha;
}
}
Of course I have defined the colorPar and colorImpar inside my own colors.xml resource.
Any Idea why this is not working??
Thanks!