gpt4 book ai didi

java - RecyclerView 返回的 ListAdapter 应该是抽象错误

转载 作者:行者123 更新时间:2023-12-02 01:47:33 25 4
gpt4 key购买 nike

UserListAdapter

public class UserListAdapter extends RecyclerView.Adapter 

出现此错误

Class UserListAdapter must either be declared abstract or implement abstract method onBindViewHolder(VH, int) in Adapter

我已经研究了有关堆栈溢出的其他答案,但没有找到任何可以修复该错误的内容。

UserListAdapter.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);
}
}

user_layout.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>
{
.......
}

干杯:)

关于java - RecyclerView 返回的 ListAdapter 应该是抽象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57446666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com