How to use ButterKnife inside adapter

后端 未结 3 1784
半阙折子戏
半阙折子戏 2021-02-13 16:30

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 WarmSpi         


        
3条回答
  •  旧巷少年郎
    2021-02-13 16:51

    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.)

    @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;
    }
    

提交回复
热议问题