我在以前的Android Studio中处理过相同的项目,但现在在此更新版本中无法对相同的项目进行编码!
我在ViewHolder(Adapter)
中编写了SetData
方法,当我试图在OnBindViewHolder{。。}
中调用此方法时,它无法识别此方法。 (查看上载图像)
当我按atl+enter时,显示“重命名引用”。
错误抛出行-34 Gist:https://Gist.github.com/reevan799/0babda6746136105f3c4fbdab1c12ae5#file-categoryadapter-java-l34
获取图像URL时出错:https://ibb.co/kq3rg3G
代码:
package com.example.generalknowledgequiz;
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()); #Error Causing Line..
}
@Override
public int getItemCount() {
return categoryModelList.size();
}
class Viewholder extends RecyclerView.ViewHolder{
private CircleImageView imageView;
private TextView title;
public Viewholder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageview);
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
在Android Studio中以红色显示(OnbindViewWholder
)
更改用于RecyclerView.Adapter
的泛型类型。 您的类声明应该是:
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.Viewholder> {
...
}
它还将要求您更改重写的方法声明。
viewholder
: @NonNull
@Override
public 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 Viewholder holder, int position) {
holder.setData(categoryModelList.get(position).getImageurl(),categoryModelList.get(position).getTitle()); #Error Causing Line..
}
setData方法是私有的