无法在适配器类中绑定ImageView

时间:2019-06-01 09:11:16

标签: java android butterknife

我在适配器中绑定ImageView时遇到问题。我应该如何正确做?

代码如下:

Override public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder;
    if (view != null) {
        holder = (ViewHolder) view.getTag();
    } else {
        view = LayoutInflater.from(getContext()).inflate(R.layout.weather_list_item, parent, false);
        holder = new ViewHolder(view);
        view.setTag(holder);
    }
    Weather model = getItem(position);
    holder.weatherIcon.setImageResource(utils.setWeatherIcon(model.getForecastID(), weatherIcon)); <- on the weatherIcon, I've got the "Cannot resolve symbol "weatherIcon"

static class ViewHolder {
    @BindView(R.id.hourly_temperature)
    TextView temperature;
    @BindView(R.id.hourly_airPressure)
    TextView airPressure;
    @BindView(R.id.hourly_airHumidity)
    TextView airHumidity;
    @BindView(R.id.hourly_windSpeed)
    TextView windSpeed;
    @BindView(R.id.hourly_time)
    TextView hour;

    @BindView(R.id.hourly_weatherIcon)
    ImageView weatherIcon;

    public ViewHolder(View view) {
        ButterKnife.bind(this, view);
    }
}

在使用ButterKnife之前,我是这样声明ImageView的:

    ImageView iv = view.findViewById(R.id.someID);
    utils.setWeatherIcon(model.getIconID(), iv);

,一切运行正常。

1 个答案:

答案 0 :(得分:0)

代替

@BindView(R.id.hourly_weatherIcon) ImageView weatherIcon;

使用

@Bind(R.id.hourly_weatherIcon) ImageView weatherIcon;