Butterknife is unable to bind inside my Adapter Class

独自空忆成欢 提交于 2019-12-03 10:32:36

I was able to solve this problem by doing the following:

First, the api docs stated the following

Be default, views are required to be present in the layout for both field and method bindings. If a view is optional add a @Nullable annotation such as the one in the support-annotations library.

@Nullable @Bind(R.id.title) TextView subtitleView;

http://jakewharton.github.io/butterknife/javadoc/

Now since I had different elements to bind coming from different xml files, I had to tag them as @Nullable because it's possible that they won't even be bindable to begin with. I changed my code to do the following :

@Nullable @Bind(R.id.drawer_row_icon)
ImageView imageView;
@Nullable @Bind(R.id.drawer_row_text)
TextView textView;
@Nullable @Bind(R.id.drawer_row_id)
FrameLayout listRow;
@Nullable @Bind(R.id.driverName)
TextView driverNameText;

While also moving the ButterKnife.bind() outside of my IF-ELSE Block.

You are binding twice. You cannot call bind twice on the same view. It is only doing your findviewbyid calls. Call bind once above your if block.

public ViewHolder(View itemView, int viewType) {
    super(itemView);

    this.viewType = viewType;

    ButterKnife.bind(this, itemView);
    if (viewType == ROW_TYPE) {
        imageView.setOnClickListener(this);
        textView.setOnClickListener(this);
        listRow.setOnClickListener(this);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!