2

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!

Andres
  • 11,439
  • 12
  • 48
  • 87

3 Answers3

2

Looks like the default background for view is transparent. Try this and it should work..

if(position%2==0){
    row.setBackgroundColor(new 
       ColorDrawable(context.getResources().getColor(R.color.colorPar)));
}else{
    row.setBackgroundColor(new 
       ColorDrawable(context.getResources().getColor(R.color.colorImpar)));
}
havexz
  • 9,550
  • 2
  • 33
  • 29
1

Use setBackgroundResource() rather than setBackgroundColor().

setBackgroundResource() takes an integer resource index as parameter, and load whatever resource that index points to (for example; a drawable, a string or in your case a color).

setBackgroundColor(), however takes an integer representing a color. That is, not a color-resource, but a direct, hexadecimal, rgba value (0xAARRGGBB).

So, when you call setBackgroundColor() using a resource index (say 7f050001, which is the first color index), you always set your color to a:127 r:5 g:0 b:1.

Jave
  • 31,598
  • 14
  • 77
  • 90
-1

In case you have more then just one or two properties you wish to change for an alternate or any other additional layout, the below post describes how to apply separated layouts based on the item index. Just override the getViewTypeCount and getViewType Methods(or in Xamarin ViewTypeCount property and GetViewType Method)

links:

Android ListView with multiple layouts

https://developer.xamarin.com/api/property/Android.Widget.BaseAdapter.ViewTypeCount/