在我的情况下如何使用黄油刀?

时间:2018-08-11 02:41:48

标签: android

在我的代码中,如果我想使用黄油刀,应该如何更改?

布局list_item是listview,因此我可以生成黄油刀注入,但是我不知道如何解决其他代码,应该使用ViewHolder吗?

 公共类GangAdapter扩展ArrayAdapter  {


公共GangAdapter(上下文上下文,ArrayList 帮派){
    超级(上下文,0,帮派);
}


@NonNull
@Override
public View getView(int position,@Nullable View convertView,@NonNull ViewGroup parent){

    如果(convertView == null){
        convertView = LayoutInflater.from(getContext())。inflate(R.layout.list_item,parent,false);
    }

    Gang currentFeature = getItem(position);


    ImageView spotImageView = convertView.findViewById(R.id.Image);
    spotImageView.setImageResource(currentFeature.getImageResourceId());

    TextView featureTextView = convertView.findViewById(R.id.where);
    featureTextView.setText(currentFeature.getFeature());

    TextView detailTextView = convertView.findViewById(R.id.about);
    detailTextView.setText(currentFeature.getExplanation());

    返回convertView;
}
 

这是我的list_item.xml。 我认为没有问题。是吗?

  






 

1 个答案:

答案 0 :(得分:2)

您可以设置注释,并在绑定时将充气视图传递为

public class GangAdapter extends ArrayAdapter<Gang> {

    @BindView(R.id.Image) ImageView spotImageView;
    @BindView(R.id.where) TextView featureTextView ;
    @BindView(R.id.about) TextView detailTextView ;
    //^^^^^^^^^^^ do the setup

    public GangAdapter(Context context, ArrayList<Gang> gangs) {
        super(context, 0, gangs);
    }


    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
            ButterKnife.bind(this, convertView);
            //                     ^^^^^^^^^^^
            // pass the view with which you want to bind the views
        }

        Gang currentFeature = getItem(position);


        spotImageView.setImageResource(currentFeature.getImageResourceId());

        featureTextView.setText(currentFeature.getFeature());

        detailTextView.setText(currentFeature.getExplanation());

        return convertView;
    }
}