Search code examples
androidlistviewcustom-lists

Colors not setting in the list


I have made a customize list in which i have to give different colors to different list items.I tried doing it but the text color is always black.I don't know why this is happening;

Code-

private Context context;
private List<CommonModel> commonList = Collections.emptyList();
private LayoutInflater inflater;
private String from;
private int[] languageColours = {R.color.green, R.color.pink, R.color.grey, R.color.chetna_red};

public CommonAdapter(Context context, List<CommonModel> commonList, String from) {
    this.context = context;
    this.commonList = commonList;
    this.from = from;
    inflater = inflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.common_custom_list_item, parent, false);
        holder.tvContent = (TextView) convertView.findViewById(R.id.tv_common_content);
        holder.ivImage = (ImageView) convertView.findViewById(R.id.iv_common_image);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.ivImage.setImageResource(commonList.get(position).getImage());
    holder.tvContent.setText(commonList.get(position).getContent());
    holder.tvContent.setTextColor(languageColours[position]);

    return convertView;
}

private class ViewHolder {

    TextView tvContent;
    ImageView ivImage;
}

I had checked that the position which i am getting is correct then to the color is not changing.


Solution

  • instead of using languageColours[position] use context.getResource.getColor(languageColours[position]);