I wrote this code RecyclerAdapter for my recyclerView in a fragment. I tried to change the background color of cards in recyclerView randomly by passing an Array of color strings to holder.cardView.setCardBackgroundColor(Color.parseColor())
This code worked and the colors changed randomly but then after few seconds the app crashed and threw the following error in the logcat
Skipped 49 frames! The application may be doing too much work on its main thread.
Here's my RecyclerAdapter.java
import android.graphics.Color;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
String[] postText;
String[] groupName;
String[] backColor = {"#ff6347", "#c2571a", "#a3b86c", "#3c6478", "#EBC94"};
public RecyclerAdapter(String[] postText, String[] groupName){
this.postText = postText;
this.groupName = groupName;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view,parent,false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
holder.txPost.setText(postText[position]);
holder.txGroupName.setText(groupName[position]);
holder.cardView.setCardBackgroundColor(Color.parseColor(backColor[position]));
}
@Override
public int getItemCount() {
return postText.length;
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
TextView txPost;
TextView txGroupName;
CardView cardView;
public RecyclerViewHolder(View view)
{
super(view);
txPost = (TextView) view.findViewById(R.id.post_text);
cardView = (CardView) itemView.findViewById(R.id.card_view);
txGroupName = (TextView) view.findViewById(R.id.group_name);
}
}
}
Why is this error occurring? And how can I fix it. Little bit of explanation will be really helpful.