Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Lists and Adapters (2015) Lists with RecyclerViews Custom Adapters and ViewHolder in RecyclerViews

Please show how to use ButterKnife in current scenario. Documentation changed.

I am able to implement ButterKnife with no errors, but when I run the app at the end of this objective I get a null pointer reference error. Also I wanted to inject hour not itemView, but it gave an error. Also the butterknife documentation shows "bind" in the place of inject.

public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {

    @InjectView(R.id.timeLabel) TextView mTimeLabel;
    @InjectView(R.id.summaryLabel) TextView mSummaryLabel;
    @InjectView(R.id.temperatureLabel) TextView mTemperatureLabel;
    @InjectView(R.id.iconImageView) ImageView mIconImageView;

    private Hour[] mHours;

    public HourAdapter(Hour[] hours) {
        mHours = hours;
    }

    @Override
    public HourViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.hourly_list_item, viewGroup, false);
        HourViewHolder viewHolder = new HourViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(HourViewHolder hourViewHolder, int i) {
        hourViewHolder.bindHour(mHours[i]);
    }

    @Override
    public int getItemCount() {
        return mHours.length;
    }

    public class HourViewHolder extends RecyclerView.ViewHolder {
        public HourViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }

        public void bindHour(Hour hour) {
            ButterKnife.inject(this,itemView);
            mTimeLabel.setText(hour.getHour());
            mSummaryLabel.setText(hour.getSummary());
            mTemperatureLabel.setText(hour.getTemperature() + "");
            mIconImageView.setImageResource(hour.getIconId());
        }
    }
}

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

If you're using version 7 of ButterKnife you'll need to change @InjectView to @Bind and ButterKnife.inject() to ButterKnife.bind().

Thanks for clarifying that! I am compiling version "6.1.0" so I should be good with using "inject."

Abhinav Kanoria
Abhinav Kanoria
7,730 Points

How do I use ButterKnife to bind views in the HourAdapter class? Where do I write this code? Please help.