How to use ButterKnife internal adapter

I would like to use ButterKnife to bind my views inside adView.

I tried this, but I can't just 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?

+5
source share
3 answers

You must pass on your ButterKnife presentation to link 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; } 

You will then have access to your views.

+5
source

ButterKnife binds your view to the ViewHolder class, so the WarmSpinnerAdapter will not 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 can either call the internal method from the adapter, or execute the logic directly inside the ViewHolder

+2
source

Since you are using an ArrayAdapter, you need to have the correct ViewHolder logic in your getView() method. (You onClick annotations are also not set correctly, as they must be placed inside the ViewHolder class.)

 @Override public 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; } 
0
source

Source: https://habr.com/ru/post/1260590/


All Articles