I have an error that says: error: cannot find symbol Vholder.setData(mValues.get(position)); ^ symbol: method setData(Object) location: variable Vholder of type ViewHolder How do I fix these?
Here is the code: This is my Adapter class for my grid layout. I do not know why it is not working.
public class TrackAdapter extends RecyclerView.Adapter {
ArrayList mValues;
Context mContext;
protected ItemListener mListener;
public TrackAdapter(Context context, ArrayList values, ItemListener itemListener) {
mValues = values;
mContext = context;
mListener=itemListener;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textView;
public ImageView imageView;
public RelativeLayout relativeLayout;
TrackData data;
public ViewHolder(View v) {
super(v);
v.setOnClickListener(this);
textView = (TextView) v.findViewById(R.id.textView);
imageView = (ImageView) v.findViewById(R.id.imageView);
relativeLayout = (RelativeLayout) v.findViewById(R.id.relativeLayout);
}
public void setData(TrackData data) {
this.data = data;
textView.setText(data.text);
imageView.setImageResource(data.icon);
relativeLayout.setBackgroundColor(Color.parseColor(data.gridBGColor));
}
@Override
public void onClick(View view) {
if (mListener != null) {
mListener.onItemClick(data);
}
}
}
@Override
public TrackAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.makati_recycler_grid_track, parent, false);
return new ViewHolder(view);
}
@Override
**public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Vholder, int position) {
Vholder.setData(mValues.get(position));
}**
@Override
public int getItemCount() {
return mValues.size();
}
public interface ItemListener {
void onItemClick(TrackData data);
}
}
or here is the line
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder Vholder, int position) {
Vholder.setData(mValues.get(position));
}
CodePudding user response:
You have to extend your TrackAdapter with RecyclerView Type. ie.
public class TrackAdapter extends RecyclerView.Adapter< ViewHolder >
enter code here
CodePudding user response:
You should cast RecyclerView.ViewHolder to TrackAdapter.ViewHolder like this:
if (Vholder instanceof TrackAdapter.ViewHolder){
((TrackAdapter.ViewHolder)Vholder).setData(mValues.get(position));
}