gpt4 book ai didi

java - Gradle 构建错误地失败,因为 "@Override method does not override method from superclass"

转载 作者:行者123 更新时间:2023-12-02 12:09:28 26 4
gpt4 key购买 nike

代码如下。 IDE 的代码没问题,但 gradle 拒绝构建,并表示:

TextAdapter is not abstract and does not override abstract method onBindViewHolder(TextAdapter.SimpleTextHolder,int) in Adapter public class TextAdapter extends RecyclerView.Adapter {

error: method does not override or implement a method from a supertype

我知道这是错误的,因为我使用 Android Studio 生成了该方法,所以我认为这一定是一个 gradle 错误?

RecyclerView适配器代码:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import ...R;
import ...jsonObjects.Nameable;

import java.util.ArrayList;

public class TextAdapter<T extends Nameable> extends RecyclerView.Adapter<TextAdapter.SimpleTextHolder> {

private ArrayList<T> items;
private ClickListener<T> clickListener;

public TextAdapter(ArrayList<T> items, ClickListener<T> onClick) {
this.items = items;
this.clickListener = onClick;
}

@Override
public SimpleTextHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.holder_text, parent, false);
return new SimpleTextHolder(view);
}

@Override
public void onBindViewHolder(SimpleTextHolder holder, int position) {
holder.itemTitle.setText(items.get(position).getName());
}


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

class SimpleTextHolder extends RecyclerView.ViewHolder {

TextView itemTitle;

SimpleTextHolder(View itemView) {
super(itemView);

itemTitle = itemView.findViewById(R.id.itemTitle);
itemView.setOnClickListener(v -> clickListener.onClick(items.get(getAdapterPosition())));
}

}

public interface ClickListener<T> {

void onClick(T item);

}

}

最佳答案

您可以通过将您的 SimpleTextHolder 类设为 static 来解决此问题,即

static class SimpleTextHolder extends RecyclerView.ViewHolder {
// ...
}

关于java - Gradle 构建错误地失败,因为 "@Override method does not override method from superclass",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46652908/

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