简体   繁体   English

在 RecyclerView.Adapter 中更改 textView 颜色

[英]Change textView color in RecyclerView.Adapter

I want to change the color of a textView in the onBindViewHolder in the Adapter depending on a variable of data.我想根据数据变量更改适配器中 onBindViewHolder 中 textView 的颜色。 I've tried this我试过这个

if (survey.getAnonymous() == 0) {
    holder.surveyIcon.setImageResource(R.drawable.ic_x);
    holder.surveyAnonymous.setTextColor(R.color.red);
} else {
    holder.surveyIcon.setImageResource(R.drawable.ic_check);
    holder.surveyAnonymous.setTextColor(R.color.green);
}

R.color.red is underlined in red and says R.color.red红色下划线并表示

Should use getResources().getColor(R.color.red)应该使用 getResources().getColor(R.color.red)

I tried that and it also gives me an error.我试过了,它也给了我一个错误。 I have tried these:我试过这些:

holder.surveyAnonymous.setTextColor(R.color.red);
holder.surveyAnonymous.setTextColor(getResources().getColor(R.color.red));
holder.surveyAnonymous.setTextColor(getColor(R.color.red));
holder.surveyAnonymous.setTextColor(Color.parseColor(R.color.red);

None of these have worked and are always underlined in red, what is the correct way to do this?这些都没有用,并且总是用红色下划线,正确的方法是什么?

int getColor (Context context, int id) int getColor (上下文上下文,int id)

 holder.surveyAnonymous.setTextColor(ContextCompat.getColor(contextObject,(R.color.red)));

FYI供参考

Color.parseColor - Parse the color string, and return the corresponding color-int. Color.parseColor - 解析颜色字符串,返回对应的color-int。

holder.surveyAnonymous.setTextColor(Color.parseColor("#54D66A");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM