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 Binding Data in the ViewHolder

Conuir Williamson
Conuir Williamson
7,825 Points

Binding widgets in ViewHolder with ButterKnife

Has anyone managed to do the 'challenge' Ben proposed of binding the controls with ButterKnife?

My code works fine with the standard public field and casting each view in the constructor, but when I change it to use @Bind, none of the items get initialised.

Even if you haven't tried it, would you have any idea why this would be happening?

Kevin Faust
Kevin Faust
15,353 Points

Do you get red squiggly lines when using @Bind?

Conuir Williamson
Conuir Williamson
7,825 Points

Nope. Everything was just as it was everywhere else I used it. Had all imports too. I wouldn't have been able to tell they weren't initialised if I had red squigglies as I wouldn't have been able to even run it. :/

Sergey Valevich
Sergey Valevich
6,041 Points

The same problem: I get the NullPointerExceptions on the views in ViewHolder.

    public class HourViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.timeLabel) TextView mTimeLabel;
        @Bind(R.id.temperatureLabel)TextView mTemperatureLabel;
        @Bind(R.id.summaryLabel)TextView mSummaryLabel;
        @Bind(R.id.iconImageView)ImageView mIconImageView;

        public HourViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(itemView);

        }

        public void bindHour (Hour hour) {
            mTemperatureLabel.setText(hour.getTemperature() + "");
            mSummaryLabel.setText(hour.getSummary());
            mIconImageView.setImageResource(hour.getIconId());
            mTimeLabel.setText(hour.getHourOfTheDay());
        }
    }

1 Answer

Hi there,

According to this site you have to call the ButterKnife.bind method with different parameters in ListViews. I also had that error and this code works fine for me:

HourViewHolder.java
public class HourViewHolder extends RecyclerView.ViewHolder {

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

        public HourViewHolder(View itemView) {
            super(itemView);

            ButterKnife.bind(this, itemView);

        }

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

Best regards,

Philip