0

When I try building a project, an error appears, indicated in the name of the topic. directs here:

 if (arrList[position].color != null){
            holder.itemView.cardView.setCardBackgroundColor(Color.parseColor(arrList[position].color))
        }else{
            holder.itemView.cardView.setCardBackgroundColor(Color.parseColor(R.color.ColorLightBlack.toString()))
        }

if I remove the condition that is written in "else", the project starts without errors. Tried changing the color, nothing changed!

Kraigolas
  • 5,121
  • 3
  • 12
  • 37
Dglasmann
  • 5
  • 2

2 Answers2

0

From this post we can see that you should write

holder.itemView.cardView
    .setCardBackgroundColor(ContextCompat.getColor(getActivity(), R.color.ColorLightBlack))

You are trying to use parseColor, which takes a hexadecimal string as an argument. But calling toString() probably isn't making the conversion to hexadecimal.

Kraigolas
  • 5,121
  • 3
  • 12
  • 37
0

R.color.ColorLightBlack is an integer value assigned by the Android System. You are trying to pass this value to parseColor method which required String. Even if you use toString on R.color.ColorLightBlack, the value still remains Integer, hence you are getting the error.

You can update the code like this:

if (arrList[position].color != null){
holder.itemView.cardView.setCardBackgroundColor(Color.parseColor(arrList[position].color)) }
else { 
holder.itemView.cardView.setCardBackgroundColor(R.color.ColorLightBlack)) 
} 
Alpha 1
  • 4,118
  • 2
  • 17
  • 23