文本颜色绑定到Viewholder的错误

时间:2018-10-25 20:55:43

标签: android android-recyclerview recycler-adapter

我有一个RecyclerView,它显示带有时间戳的项目列表。我希望每个查看器中的一个TextView的文本基于每个项目的时间戳都是不同的颜色。 Viewholder似乎在日志中正确绑定,并且正确的颜色在Android Studio的左窗格中显示,但是设备上的文本颜色错误:

enter image description here

这是我的适配器的相关代码:

 @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;

}

还有我的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>

1 个答案:

答案 0 :(得分:2)

TextView.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);