我有一个自定义适配器来填充ListView
一个名称和一个数字。如果该数字具有特定的价值,则该数字具有不同的颜色。当它显示时它工作正常,但是当我滚动列表时,其他不具备条件的数字也会获得颜色。这是自定义适配器和getView()
方法。
private class MyCustomAdapter extends ArrayAdapter<CategoriaD> {
private ArrayList<CategoriaD> elementList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<CategoriaD> elementList) {
super(context, textViewResourceId, elementList);
this.elementList = new ArrayList<CategoriaD>();
this.elementList.addAll(elementList);
}
private class ViewHolder {
TextView texto;
TextView cantidad;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.activity_inspeccion_categoria, null);
holder = new ViewHolder();
holder.texto = (TextView) convertView.findViewById(R.id.texto);
holder.cantidad = (TextView) convertView.findViewById(R.id.estadistica);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
CategoriaD Elemento = elementList.get(position);
holder.texto.setText(Elemento.getNombre());
holder.cantidad.setText(Elemento.getEstadistica());
if(Elemento.getEstadistica().equals(“50”)){
holder.cantidad.setTextColor(Color.RED);
}
holder.cantidad.setTag(Elemento);
return convertView;
}
}