Android 绑定到Viewholder的文本颜色错误

Android 绑定到Viewholder的文本颜色错误,android,android-recyclerview,Android,Android Recyclerview,我有一个RecyclerView展示了一个带有时间戳的项目列表。我希望每个viewholder中的一个TextView的文本根据每个项目的时间戳具有不同的颜色。Viewholder在日志中似乎已正确绑定,Android Studio的左侧窗格中显示了正确的颜色,但设备上的文本颜色错误: 以下是我的适配器的相关代码: @Override public void onBindViewHolder(@NonNull ChatViewHolder holder, int position)

我有一个RecyclerView展示了一个带有时间戳的项目列表。我希望每个viewholder中的一个TextView的文本根据每个项目的时间戳具有不同的颜色。Viewholder在日志中似乎已正确绑定,Android Studio的左侧窗格中显示了正确的颜色,但设备上的文本颜色错误:

以下是我的适配器的相关代码:

 @Override
    public void onBindViewHolder(@NonNull ChatViewHolder holder, int position) {

    Log.d(TAG, "OnbindViewholder Called");
    Message message = messageList.get(position);
    int authorColor = getAuthorColor(Calendar.getInstance().getTime(), message.getTimestamp());

    Log.d(TAG,Integer.toString(authorColor));

    holder.message.setText(message.getMessage());
    holder.author.setText(message.getAuthor() + ":");
    holder.author.setTextColor(authorColor);

}

public class ChatViewHolder extends RecyclerView.ViewHolder {

    TextView author;
    TextView message;
    RelativeLayout singleMessageContainer;

    public ChatViewHolder(View itemView) {
        super(itemView);

        author = itemView.findViewById(R.id.chatAuthor);
        message = itemView.findViewById(R.id.chatMessage);
        singleMessageContainer = itemView.findViewById(R.id.singleMessageContainer);
        author.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createDMActivity(author.getText().toString());
            }
        });

    }
}

private int getAuthorColor(Date currentDate, Date messageDate) {
    int authorColorResourceID = 1;
    long timeAgo = currentDate.getTime() / 60000 - messageDate.getTime() / 60000;

    Log.d(TAG, "Time Ago = " + Long.toString(timeAgo));

    if (timeAgo < 15) {
        authorColorResourceID = R.color.Accent3;

    }
    if (timeAgo >= 15 && timeAgo < 30) {
        authorColorResourceID = R.color.chatLast30;
    }

    if (timeAgo >= 30 && timeAgo < 60) {
        authorColorResourceID = R.color.chatLastHour;
    }
    if (timeAgo >= 60 && timeAgo < 180) {
        authorColorResourceID = R.color.chatLast3Hours;
    }
    if (timeAgo >= 180) {
        authorColorResourceID = R.color.chatOver3Hours;
    }

    Log.d(TAG, Integer.toString(authorColorResourceID));


    return authorColorResourceID;

}
@覆盖
public void onBindViewHolder(@NonNull ChatViewHolder,int位置){
Log.d(标记“OnbindViewholder Called”);
Message Message=messageList.get(位置);
int authorColor=getAuthorColor(Calendar.getInstance().getTime(),message.getTimestamp());
Log.d(TAG,Integer.toString(authorColor));
holder.message.setText(message.getMessage());
holder.author.setText(message.getAuthor()+“:”);
holder.author.setTextColor(authorColor);
}
公共类ChatViewHolder扩展了RecyclerView.ViewHolder{
文本视图作者;
文本视图消息;
RelativeLayout singleMessageContainer;
公共聊天室视图持有者(查看项目视图){
超级(项目视图);
author=itemView.findviewbyd(R.id.chatAuthor);
message=itemView.findviewbyd(R.id.chatMessage);
singleMessageContainer=itemView.findViewById(R.id.singleMessageContainer);
author.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
CreatedActivity(author.getText().toString());
}
});
}
}
private int getAuthorColor(日期currentDate、日期messageDate){
int authorColorResourceID=1;
long-timeAgo=currentDate.getTime()/60000-messageDate.getTime()/60000;
Log.d(标记,“Time Ago=“+Long.toString(timeAgo));
如果(时间<15){
authorColorResourceID=R.color.Accent3;
}
如果(timeAgo>=15&&timeAgo<30){
authorColorResourceID=R.color.chatLast30;
}
如果(时间间隔>=30&&timeAgo<60){
authorColorResourceID=R.color.chatLastHour;
}
如果(时间间隔>=60&&timeAgo<180){
authorColorResourceID=R.color.chatlast3小时;
}
如果(时间间隔>=180){
authorColorResourceID=R.color.chatover3小时;
}
Log.d(TAG,Integer.toString(authorColorResourceID));
返回authorColorResourceID;
}
和my colors.xml文件:

<resources>
<color name="colorPrimary">#6247aa</color>
<color name="colorPrimaryDark">#1b1721</color>
<color name="colorAccent">#bdede0</color>

<color name="yellow">#f1c40f</color>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
<color name="green">#27AE60</color>
<color name="light_purple">#E8E1E7</color>
<color name="light_green">#50d050</color>
<color name="dark_green">#008000</color>
<color name="Accent">#bdede0</color>
<color name="lightBackground">#e0e0e0</color>
<color name="text_color_primary">#ddffffff</color>
<color name="hintColor">#999999</color>
<color name="Accent2">#bb90fe</color>
<color name="Accent3">#55d6be</color>
<color name="chatLast30">#3f9b89</color>
<color name="chatLastHour">#2e7164</color>
<color name="chatLast3Hours">#24584e</color>
<color name="chatOver3Hours">#1c413b</color>

#6247aa
#1b1721
#bdede0
#f1c40f
#ffffff
#000000
#27AE60
#E8E1E7
#50d050
#008000
#bdede0
#e0e0e0
#ddffffff
#999999
#bb90fe
#55d6be
#3f9b89
#2e7164
#24584e
#1c413b

文本视图。setTextColor()接受颜色值。您正在向它传递颜色资源id。不幸的是,两者都表示为
int
,因此编译器无法判断是否发生了此错误

要修复此问题,请在调用
setTextColor()
之前将颜色资源id解析为颜色值:

int authorColorId = getAuthorColor(Calendar.getInstance().getTime(), message.getTimestamp());
int authorColor = ContextCompat.getColor(holder.itemView.getContext(), authorColorId);
holder.author.setTextColor(authorColor);