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 trialMichael Partridge
7,129 PointsPlease 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
56,414 PointsIf you're using version 7 of ButterKnife you'll need to change @InjectView to @Bind and ButterKnife.inject() to ButterKnife.bind().
Michael Partridge
7,129 PointsMichael Partridge
7,129 PointsThanks for clarifying that! I am compiling version "6.1.0" so I should be good with using "inject."
Abhinav Kanoria
7,730 PointsAbhinav Kanoria
7,730 PointsHow do I use ButterKnife to bind views in the HourAdapter class? Where do I write this code? Please help.