gpt4 book ai didi

android - 在我的案例中如何使用 butterknife?

转载 作者:行者123 更新时间:2023-11-29 18:46:14 24 4
gpt4 key购买 nike

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

Layout list_item是listview,所以我可以生成butterknife注入(inject),但我不知道如何修复其他代码,我应该使用ViewHolder吗?

public class GangAdapter extends ArrayAdapter<Gang> {


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

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());

return convertView;
}

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:id="@+id/where"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="24sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left" />

最佳答案

您可以设置注释并在绑定(bind)时将膨胀 View 传递给

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

关于android - 在我的案例中如何使用 butterknife?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51796011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com