How to use ButterKnife inside adapter

Stepan picture Stepan · Nov 30, 2016 · Viewed 10.1k times · Source

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?

Answer

omerkaraca picture omerkaraca · Jan 11, 2017

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

@Override
public 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.