Lori wayne
Lori wayne

Reputation: 35

onbindViewHolder is not accepting view holder variables

For some reason the on bindviewholder is not accepting the variables created by Viewholder(holder.name and holder.dexription). I have tried everything to fix it but nothing has worked Here is the code if you could help I would be grateful.

public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<City> cities;
        public CityAdapter(ArrayList<City> cities) {
            this.cities =cities;
        }

    @Override
    public int getItemCount() {
        if (cities != null) {
            return cities.size();
        } else {
            return 0;
        }
    }

    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
       View v =(View) LayoutInflater.from(parent.getContext()).inflate(R.layout.items,parent,false);
       return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        City city = cities.get(position);

        holder.name.setText(city.getName());
        holder.description.setText(city.getDescription());
    }
    static class ViewHolder extends RecyclerView.ViewHolder {

        public View view;
        public TextView name;
        public TextView description;
        public ImageView image;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            this.name=(TextView) itemView.findViewById(R.id.name);
            this.description=(TextView) itemView.findViewById(R.id.description);
            this.image=(ImageView) itemView.findViewById(R.id.image);
        }
    }    
}

Upvotes: 2

Views: 1073

Answers (1)

Mohammed Junaid
Mohammed Junaid

Reputation: 1412

You are extending from wrong class

change CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> to

CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder>

And change your "onBindViewHolder" method's first argument from

RecyclerView.ViewHolder

to

ViewHolder

Upvotes: 4

Related Questions