Android 卡片布局和回收视图可以';无法在viewholder中获取我的视图元素

Android 卡片布局和回收视图可以';无法在viewholder中获取我的视图元素,android,android-recyclerview,adapter,android-cardview,Android,Android Recyclerview,Adapter,Android Cardview,我正在尝试用一个适配器构建一个简单的循环视图,我将按照教程来归档它 当我在适配器中设置所有内容时,我得到一个错误,说我需要将所有元素(TextView和那些)转换为静态字段,作者不需要,我也不知道为什么 这是我的代码: public class SimiliarPlantsAdapter extends RecyclerView.Adapter<SimiliarPlantsAdapter.PlantViewHolder>{ ArrayList<Plant> plants

我正在尝试用一个适配器构建一个简单的循环视图,我将按照教程来归档它

当我在适配器中设置所有内容时,我得到一个错误,说我需要将所有元素(TextView和那些)转换为静态字段,作者不需要,我也不知道为什么

这是我的代码:

public class SimiliarPlantsAdapter extends RecyclerView.Adapter<SimiliarPlantsAdapter.PlantViewHolder>{

ArrayList<Plant> plants = new ArrayList<Plant>();

public static class PlantViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView plantName;
    TextView plantCheck;
    ImageView plantPhoto;

    PlantViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.cv);
        plantName = (TextView)itemView.findViewById(R.id.plantName);
        plantCheck = (TextView)itemView.findViewById(R.id.plantCheck);
        plantPhoto = (ImageView)itemView.findViewById(R.id.plantPhoto);
    }

}

@Override
public PlantViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.similiar_photo_row, viewGroup, false);
    PlantViewHolder pvh = new PlantViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(PlantViewHolder holder, int position) {
    PlantViewHolder.plantName.setText(plants.get(position).getSpecie());
    PlantViewHolder.plantCheck.setText("Are you sure this is the plant?");
    PlantViewHolder.personPhoto.setImageResource(plants.get(position).photoId);
}


@Override
public int getItemCount() {
    return plants.size();
}

public SimiliarPlantsAdapter(ArrayList<Plant> plants) {
    this.plants = plants;
}
我的plantName和plantCheck不起作用,我需要将初始化值转换为静态字段,为什么会发生这种情况,有什么提示吗


谢谢

更改了关于BindViewHolder的方法我认为您在 PlantViewHolder类错误。请这样做

@Override
public void onBindViewHolder(PlantViewHolder holder, int position) {
    holder.plantName.setText(plants.get(position).getSpecie());
    holder.plantCheck.setText("Are you sure this is the plant?");
}
您不必使用
static
holder对象已在方法中传递请尝试使用传递的引用访问其变量
e

更改onBindViewHolder方法我认为您在 PlantViewHolder类错误。请这样做

@Override
public void onBindViewHolder(PlantViewHolder holder, int position) {
    holder.plantName.setText(plants.get(position).getSpecie());
    holder.plantCheck.setText("Are you sure this is the plant?");
}
您不必使用
static
holder对象已在方法中传递请尝试使用传递的引用访问其变量
e

使用
PlantViewHolder的
对象
holder
设置
视图
属性

更新
onBindViewHolder
如下所示:

@Override
    public void onBindViewHolder(PlantViewHolder holder, int position) {
        PlantViewHolder.plantName.setText(plants.get(position).getSpecie());
        PlantViewHolder.plantCheck.setText("Are you sure this is the plant?");
        PlantViewHolder.personPhoto.setImageResource(plants.get(position).photoId);
    }
@Override
public void onBindViewHolder(PlantViewHolder holder, int position) {
    holder.plantName.setText(plants.get(position).getSpecie());
    holder.plantCheck.setText("Are you sure this is the plant?");
    holder.personPhoto.setImageResource(plants.get(position).photoId);
}

使用
PlantViewHolder的
对象
holder
设置
视图
属性

更新
onBindViewHolder
如下所示:

@Override
    public void onBindViewHolder(PlantViewHolder holder, int position) {
        PlantViewHolder.plantName.setText(plants.get(position).getSpecie());
        PlantViewHolder.plantCheck.setText("Are you sure this is the plant?");
        PlantViewHolder.personPhoto.setImageResource(plants.get(position).photoId);
    }
@Override
public void onBindViewHolder(PlantViewHolder holder, int position) {
    holder.plantName.setText(plants.get(position).getSpecie());
    holder.plantCheck.setText("Are you sure this is the plant?");
    holder.personPhoto.setImageResource(plants.get(position).photoId);
}

我的回答是,我笨到连伤害都没有,谢谢你的帮助:我的回答是,我笨到连伤害都没有,谢谢你的帮助:DTy的帮助,但是你的答案和上面的一样,但是你的答案和上面的一样