具有不同项目背景颜色的 RecyclerView

RecyclerView with different background colors of items

我需要让我的 RecyclerView 项目的 TextView 显示多种背景颜色。我有 5 种不同的颜色,我需要在每 5 件商品后显示。 这是我的代码我会知道哪里出了问题? 我有这个错误:

java.lang.NullPointerException:尝试在 android.support 处的空对象引用上调用虚方法 'int android.content.Context.getColor(int)'。v4.content.ContextCompat.getColor(ContextCompat.java:409)

public class AdapterHumeurs extends RecyclerView.Adapter<AdapterHumeurs.MyViewholder> {

    List<Humeur> listArray;
    Context context;

    public AdapterHumeurs(List<Humeur> List){
        this.listArray = List;
    }
    @Override
        public void onBindViewHolder(MyViewholder holder, int position) {

            Humeur data = listArray.get ( position );

            if(position % 5 == 0){
                holder.TextLabelHumeur.setBackgroundColor(ContextCompat.getColor(context,R.color.color1));
            }else if(position % 5 == 1){
                holder.TextLabelHumeur.setBackgroundColor(ContextCompat.getColor(context,R.color.color2));
            }else if(position % 5 == 2){
                holder.TextLabelHumeur.setBackgroundColor(ContextCompat.getColor(context,R.color.color3));
            }else if(position % 5 == 3){
                holder.TextLabelHumeur.setBackgroundColor(ContextCompat.getColor(context,R.color.color4));
            }else if(position % 5 == 4){
                holder.TextLabelHumeur.setBackgroundColor(ContextCompat.getColor(context,R.color.color5));
            }

            holder.TextLabelHumeur.setText ( data.getLabelHumeur ( ) );
        }
}

此处未初始化上下文。您可以通过 thisActivityName.this在 activity.

中创建适配器对象时访问适配器 class 中的上下文