I am using ArrayAdapter to populate a listview using viewholder. I follow the answer in Android Alternate row Colors in ListView but I get the same color at the 5th and 6th row, 10th and 11th row, 15th and 16th row and so on. Here is my code of getView method inside the ArrayAdapter class
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.vaccine_row, parent, false);
if (position % 2 == 0) {
convertView.setBackgroundColor(Color.BLUE);
}
vh = new ViewHolder();
vh.brandnametv = (TextView) convertView.findViewById(R.id.vaccinebrand);
vh.genericnametv = (TextView) convertView.findViewById(R.id.vaccinegeneric);
vh.companytv = (TextView) convertView.findViewById(R.id.company);
vh.schedule1tv = (TextView) convertView.findViewById(R.id.schedule1);
vh.schedule2tv = (TextView) convertView.findViewById(R.id.schedule2);
vh.abbreviation = (TextView) convertView.findViewById(R.id.abbreviation);
vh.vaccinetypeandroute = (TextView) convertView.findViewById(R.id.vaccinetypeandroute);
convertView.setTag(vh);
} else vh = (ViewHolder) convertView.getTag();
vaccineItem di = itemsArrayList.get(position);
vh.brandnametv.setText(di.getBrandname());
vh.genericnametv.setText(di.getGenericname());
vh.companytv.setText(di.getCompany());
vh.schedule1tv.setText(di.getSchedule1());
vh.schedule2tv.setText(di.getSchedule2());
vh.abbreviation.setText(di.getAbbreviation());
vh.vaccinetypeandroute.setText(di.getVaccinetypeandroute());
return convertView;
}
static class ViewHolder {
private TextView brandnametv;
private TextView genericnametv;
private TextView companytv;
private TextView schedule1tv;
private TextView schedule2tv;
private TextView abbreviation;
private TextView vaccinetypeandroute;
}