I would like to use ButterKnife to bind my views inside listView adpater.
I tried this, but i can not simply use my "spinner" var.
public class WarmSpinnerAdapter extends ArrayAdapter<Warm> {
Context context;
public WarmSpinnerAdapter(Context context, int resource, Warm[] objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.item_spinner, null);
return v;
}
@OnClick(R.id.spinner)
public void onClick() {
//open dialog and select
}
static class ViewHolder {
@BindView(R.id.spinner)
MyTextView spinner;
ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
Any ideas please?
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.)ButterKnife is binding your view to the
ViewHolder
class, soWarmSpinnerAdapter
won't be able to access it directly. Instead, you should move this part inside theViewHolder
class:From there, you could either call an internal method from the adapter or execute the logic directly inside the
ViewHolder
You should pass your view to ButterKnife to bind it first.
Then you will have access your Views.