gpt4 book ai didi

android - ListView 中的备用项目颜色

转载 作者:行者123 更新时间:2023-11-29 00:42:40 25 4
gpt4 key购买 nike

我正在尝试更改 android 中 ListView 的项目颜色,但我无法使其工作。我希望颜色交替...我制作了自己的适配器并在 getView 方法中我在返回 ListView 之前更改了颜色但是它不起作用我不知道为什么...

这是我的代码:

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;
}
}

当然,我已经在我自己的 colors.xml 资源中定义了 colorPar 和 colorImpar。

知道为什么这不起作用吗?

谢谢!

最佳答案

看起来 View 的默认背景是透明的。试试这个,它应该可以工作..

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)));
}

关于android - ListView 中的备用项目颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8438450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com