How to use ButterKnife inside adapter How to use ButterKnife inside adapter android android

How to use ButterKnife inside adapter


You should pass your view to ButterKnife to bind it first.

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {    View v = LayoutInflater.from(context).inflate(R.layout.item_spinner, null);    ButterKnife.bind(this,v);    return v;}

Then you will have access to your Views.


ButterKnife is binding your view to the ViewHolder class, so WarmSpinnerAdapter won't be able to access it directly. Instead, you should move this part inside the ViewHolder class:

@OnClick(R.id.spinner)public void onClick() {    //open dialog and select}

From there, you could either call an internal method from the adapter or execute the logic directly inside the ViewHolder


Since you're using an ArrayAdapter you need to have the proper ViewHolder logic in your getView() method. (You're onClick annotation is also not set correctly as it should be placed inside the ViewHolder class.)

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {    ViewHolder viewHolder;    if (convertView == null) {        convertView = LayoutInflater.from(context).inflate(R.layout.item_spinner, null);        viewHolder = new ViewHolder(convertView);        convertView.setTag(viewHolder);    } else {        viewHolder = (ViewHolder) convertView.getTag();    }    // now you can access your spinner var.    MyTextView spinner = viewHolder.spinner;    return convertView;}