颜色未在列表中设置
Colors not setting in the list
我制作了一个自定义列表,其中我必须为不同的列表赋予不同的颜色items.I尝试这样做但文本颜色总是black.I不知道为什么会这样;
代码-
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;
}
我已经检查过我得到的位置是正确的然后颜色没有改变。
而不是使用 languageColours[position]
使用 context.getResource.getColor(languageColours[position]);
就用这个
holder.tvContent.setTextColor(R.color.Red);
我制作了一个自定义列表,其中我必须为不同的列表赋予不同的颜色items.I尝试这样做但文本颜色总是black.I不知道为什么会这样;
代码-
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;
}
我已经检查过我得到的位置是正确的然后颜色没有改变。
而不是使用 languageColours[position]
使用 context.getResource.getColor(languageColours[position]);
就用这个
holder.tvContent.setTextColor(R.color.Red);