在 Recyclerview 中访问另一个 viewholder 时,变量 holder 出错
Getting error in variable holder on accessing another view holder in Recycler View
我在 java class 文件
中的 getItemCount()
中声明了 2 个字符串
查看我的代码
package com.example.quizapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<CategoryModel> categoryModelList;
public CategoryAdapter(List<CategoryModel> categoryModelList) {
this.categoryModelList = categoryModelList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item,parent,false);
return new viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
}
@Override
public int getItemCount() {
return categoryModelList.size();
}
class viewholder extends RecyclerView.ViewHolder{
private CircleImageView imageView;
private TextView title;
private String url;
public viewholder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
title = itemView.findViewById(R.id.title);
}
private void setData(String url, String title){
Glide.with(itemView.getContext()).load(url).into(imageView);
this.title.setText(title);
}
}
}
我必须创建一个方法 setData
并且在这个方法中,传递了两个字符串数据类型 URL
和 title
但是当我试图访问它们时两者都不起作用这个方法
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
}
这在 onBindViewHolder
中不起作用
错误图片
错误信息:
error: cannot find symbol
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
^
symbol: method setData(String,String)
location: variable holder of type ViewHolder
根本原因
setData()
是viewholder
class的一个方法,但是在onBindViewHolder
里面,holder
变量是RecyclerView.ViewHolder
的一个实例,这就是编译器抛出该错误的原因。
解决方案
将 holder
投射到您的 viewholder
class。从
更改您的代码
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
到
String url = categoryModelList.get(position).getImageUrl();
String title = categoryModelList.get(position).getTitle();
((viewholder) holder).setData(url, title);
我在 java class 文件
中的getItemCount()
中声明了 2 个字符串
查看我的代码
package com.example.quizapp;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<CategoryModel> categoryModelList;
public CategoryAdapter(List<CategoryModel> categoryModelList) {
this.categoryModelList = categoryModelList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_item,parent,false);
return new viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
}
@Override
public int getItemCount() {
return categoryModelList.size();
}
class viewholder extends RecyclerView.ViewHolder{
private CircleImageView imageView;
private TextView title;
private String url;
public viewholder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.image_view);
title = itemView.findViewById(R.id.title);
}
private void setData(String url, String title){
Glide.with(itemView.getContext()).load(url).into(imageView);
this.title.setText(title);
}
}
}
我必须创建一个方法 setData
并且在这个方法中,传递了两个字符串数据类型 URL
和 title
但是当我试图访问它们时两者都不起作用这个方法
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
}
这在 onBindViewHolder
错误图片
错误信息:
error: cannot find symbol
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
^
symbol: method setData(String,String)
location: variable holder of type ViewHolder
根本原因
setData()
是viewholder
class的一个方法,但是在onBindViewHolder
里面,holder
变量是RecyclerView.ViewHolder
的一个实例,这就是编译器抛出该错误的原因。
解决方案
将 holder
投射到您的 viewholder
class。从
holder.setData(categoryModelList.get(position).getImageUrl(),categoryModelList.get(position).getTitle());
到
String url = categoryModelList.get(position).getImageUrl();
String title = categoryModelList.get(position).getTitle();
((viewholder) holder).setData(url, title);