java - RecyclerView 返回的 ListAdapter 应该是抽象错误
问题描述
在里面UserListAdapter
public class UserListAdapter extends RecyclerView.Adapter
给出这个错误
Class UserListAdapter must either be declared abstract or implement abstract method onBindViewHolder(VH, int) in Adapter
我已经查看了有关堆栈溢出的其他答案,但我没有找到任何修复错误的方法。
用户列表适配器.java
public class UserListAdapter extends RecyclerView.Adapter {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
// data is passed into the constructor
UserListAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
// inflates the row layout from xml when needed
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.user_layout, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData.get(position);
holder.myTextView.setText(animal);
}
// total number of rows
public int getItemCount() {
return mData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.txtStudentNameSubjectInfoPage);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
用户布局.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp">
<ImageView
android:id="@+id/imgStudentIconSubjectInfoPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars[13]" />
<TextView
android:id="@+id/txtStudentNameSubjectInfoPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="User Name"
android:textAlignment="center"
android:textColor="#292929"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintStart_toEndOf="@+id/imgStudentIconSubjectInfoPage"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_subject_info_page.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SubjectInfoPage">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="@+id/tableLayout"
tools:layout_editor_absoluteX="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
解决方案
父类与您的自定义定义与它自己RecyclerView.Adapter
的混淆。Viewholder
如果你想使用你的,扩展RecyclerView.Adapter
如下
public class UserListAdapter extends RecyclerView.Adapter<UserListAdapter.ViewHolder>
{
.......
}
干杯:)
推荐阅读
- javascript - Javascript中的SQL语句
- http - 使用 Inno Setup 使用 POST 请求发送用户名和密码
- java - 如何将浮点数转换为 int 并考虑其小数?
- hibernate - 如果Id没有equals,hibernate如何在同一个会话中找到相同的对象
- git - Git删除子模块
- python - Scrapy 和 xpath 怪异——自动添加标签、轴和步骤?
- doctrine-orm - 教义双向关联错误。内存耗尽
- php - 我正在尝试在 php 中为日期添加一个月,日期是通过 datepicker YYY-MM-DD 格式选择的
- php - PHP - 试图从数组中获取一个值(api输出) - 循环困难
- git - 为什么这不起作用:'git log 分支名称:文件路径'?