【问题标题】:cannot reslove symbol 'position' [duplicate]无法解析符号“位置”[重复]
【发布时间】:2020-01-09 06:13:57
【问题描述】:

我正在编写一个适配器类,其中 get 位置显示错误:

onBindViewHolder 中的“无法解析符号‘位置’”。

public class cat_adapter extends RecyclerView.Adapter<cat_adapter.Viewholder> {

private List<cat_model> cat_modelList;

public cat_adapter(List<cat_model> cat_modelList) {
    this.cat_modelList = cat_modelList;
}


@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category,,false);
    return new Viewholder(view);
}

@Override
public void onBindViewHolder(@NonNull Viewholder holder, int i) {
    holder.setData(cat_modelList.get(position).getCategory();

}

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

class Viewholder extends RecyclerView.ViewHolder{

    private TextView title;

    public Viewholder(@NonNull View itemView) {
        super(itemView);

        title= itemView.findViewById(R.id.category);
    }

    private void setData(String title){
        this.title.setText(title);
    }
}


}

【问题讨论】:

    标签: java android-studio position android-adapter android-viewholder


    【解决方案1】:

    像这样更改代码。它会起作用的。您必须将 'position' 替换为 'i'。

    @Override
    public void onBindViewHolder(@NonNull Viewholder holder, int i) {
        holder.setData(cat_modelList.get(i).getCategory();
    
    }
    

    你必须传递你从 onBindViewHolder 方法的第二个参数得到的变量,它是一个 int 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      • 2019-03-26
      • 2017-04-24
      • 1970-01-01
      相关资源
      最近更新 更多