0

In my Listview I want to have TextViews with image Drawables colored accordingly to the text. I tried using the code below but while the text is showing as expected, the Drawable always shows the last color in the list for all of the TextViews - until the list is scrolled, then it shows the right color.

 @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = convertView;
    if (row == null) {
        row = inflater.inflate(R.layout.mylayout, parent, false);
        holder = new ViewHolder();
        holder.textView = (TextView) row.findViewById(R.id.myTextView);
        holder.img = getResources().getDrawable(R.drawable.my_icon);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    holder.textView.setText(colors[position]);

    int color = 0;
    switch (colors[position])
    {
        case "blue":
            color = getResources().getColor(R.color.blue);
            break;
        case "green":
            color = getResources().getColor(R.color.green);
            break;
        case "red":
            color = getResources().getColor(R.color.red);
            break;
    }

    holder.img.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    holder.textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, holder.img);
    return row;
}
static class ViewHolder {
    TextView textView;
    Drawable img;
}
james
  • 221
  • 1
  • 5
  • 14

1 Answers1

0

why you are trying to point holder.img to a drawable image. you should use a view and setbackgroundcolour as it

Sangeet Suresh
  • 2,527
  • 1
  • 18
  • 19